Files
BPI-CM9-Linux/aml-comp/vendor/amlogic/meson_videoserver/display/DispAdptLog.cpp
T

146 lines
3.5 KiB
C++
Raw Normal View History

2026-08-13 11:28:40 +08:00
/*
* Copyright (C) 2024 Amlogic Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <fcntl.h>
#include <sys/time.h>
#include "DispAdptLog.h"
static int g_activeLevel = 1;
FILE *g_file = NULL;
FILE *getDebugFile()
{
if (!g_file)
return stderr;
return g_file;
}
long long DispAdptGetCurrentTime(void)
{
long long utcCurrentTimeMillis;
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
utcCurrentTimeMillis = ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
return utcCurrentTimeMillis;
}
void DispAdptLog(int level, const char *fmt, ...)
{
if (level <= g_activeLevel) {
va_list argptr;
fprintf(getDebugFile(), "%lld: pid: %d ", DispAdptGetCurrentTime(), getpid());
va_start(argptr, fmt);
vfprintf(getDebugFile(), fmt, argptr);
fflush(getDebugFile());
va_end(argptr);
}
}
void DispAdptLogInit()
{
const char *env = getenv("DISPLAY_DEBUG_FILE");
if (env && g_file == NULL) {
if (!strcmp(env, "stdout"))
g_file = stdout;
else
g_file = fopen(env, "w");
}
env = getenv("DISPLAY_DEBUG");
if (env) {
g_activeLevel = atoi(env);
}
}
int DispAdptReadFile(const char *path, char *val, bool original, int valSize)
{
char buf[MESON_MAX_STR_LEN] = { 0 };
int fd, len;
if ((fd = open(path, O_RDONLY)) < 0) {
DISPLAY_LOGE("open %s fail. Error info [%s]", path, strerror(errno));
return -errno;
}
len = read(fd, buf, MESON_MAX_STR_LEN - 1);
close(fd);
if (len < 0) {
DISPLAY_LOGE("read error: %s, %s\n", path, strerror(errno));
return -errno;
}
if (!original) {
int i, j;
for (i = 0, j = 0; i <= len - 1; i++) {
/*
* change '\0' to 0x20(spacing), otherwise the string buffer will be cut off
* if the last char is '\0' should not replace it
*/
if (0x0 == buf[i] && i < len - 1) {
buf[i] = 0x20;
DISPLAY_LOGD("read buffer index:%d is a 0x0, replace to spacing \n", i);
}
/* delete all the character of '\n' and '\r'*/
if (0x0a != buf[i] && 0x0d != buf[i]) {
buf[j++] = buf[i];
}
}
buf[j] = 0x0;
}
DISPLAY_LOGD("read %s, result length:%d, val:%s\n", path, len, buf);
strncpy(val, buf, valSize);
return 0;
}
int DispAdptWriteFile(const char *path, const char *val)
{
int fd;
if ((fd = open(path, O_RDWR)) < 0) {
DISPLAY_LOGE("open %s fail. Error info [%s]", path, strerror(errno));
return -errno;
}
DISPLAY_LOGD("%s: write (%s) to file (%s)", __func__, val, path);
if (write(fd, val, strlen(val)) != (int)strlen(val))
DISPLAY_LOGE("write %s failed!\n", path);
close(fd);
return 0;
}
void DispAdptLogDestroy()
{
if (g_file != NULL)
fclose(g_file);
}