init_log.c 5.2 KB
Newer Older
Z
zhong_ning 已提交
1
/*
M
Mupceet 已提交
2
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
Z
zhong_ning 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * 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 "init_log.h"
X
xionglei6 已提交
17

Z
zhong_ning 已提交
18 19 20
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
Z
zhong_ning 已提交
21
#include <sys/stat.h>
Z
zhong_ning 已提交
22
#include <time.h>
M
Mupceet 已提交
23
#include <sys/time.h>
Z
zhong_ning 已提交
24

C
cheng_jinsong 已提交
25
#include "init_utils.h"
X
xionglei6 已提交
26
#include "securec.h"
X
fix log  
xionglei6 已提交
27
#ifdef OHOS_LITE
X
xionglei6 已提交
28 29 30
#ifndef INIT_LOG_INIT
#define INIT_LOG_INIT LOG_CORE
#endif
X
fix log  
xionglei6 已提交
31 32 33 34 35
#include "hilog/log.h"
#endif
#ifdef INIT_AGENT
#include "hilog_base/log_base.h"
#endif
Z
zhong_ning 已提交
36

M
Mupceet 已提交
37
#define DEF_LOG_SIZE 128
Z
zhong_ning 已提交
38 39
#define BASE_YEAR 1900

Z
zhong_ning 已提交
40
static InitLogLevel g_logLevel = INIT_INFO;
X
fix log  
xionglei6 已提交
41
#ifdef INIT_FILE
X
xionglei6 已提交
42
static void LogToFile(const char *logFile, const char *tag, const char *info)
X
xionglei6 已提交
43
{
M
Mupceet 已提交
44 45
    struct timespec curr;
    if (clock_gettime(CLOCK_REALTIME, &curr) != 0) {
Z
zhong_ning 已提交
46 47
        return;
    }
L
laiguizhong 已提交
48 49
    FILE *outfile = NULL;
    INIT_CHECK_ONLY_RETURN((outfile = fopen(logFile, "a+")) != NULL);
M
Mupceet 已提交
50
    struct tm t;
M
codex  
Mupceet 已提交
51 52 53 54
    char dateTime[80] = {"00-00-00 00:00:00"}; // 80 data time
    if (localtime_r(&curr.tv_sec, &t) != NULL) {
        strftime(dateTime, sizeof(dateTime), "%Y-%m-%d %H:%M:%S", &t);
    }
M
Mupceet 已提交
55
    (void)fprintf(outfile, "[%s.%ld][pid=%d %d][%s]%s \n", dateTime, curr.tv_nsec, getpid(), gettid(), tag, info);
X
fix log  
xionglei6 已提交
56 57
    (void)fflush(outfile);
    fclose(outfile);
Z
zhong_ning 已提交
58 59 60 61
    return;
}
#endif

X
fix log  
xionglei6 已提交
62
#ifdef INIT_DMESG
Z
zhong_ning 已提交
63
static int g_fd = -1;
M
Mupceet 已提交
64
INIT_LOCAL_API void OpenLogDevice(void)
Z
zhong_ning 已提交
65
{
X
xionglei6 已提交
66
    int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
Z
zhong_ning 已提交
67 68
    if (fd >= 0) {
        g_fd = fd;
Z
zhong_ning 已提交
69
    }
Z
zhong_ning 已提交
70 71 72
    return;
}

X
xionglei6 已提交
73
void LogToDmesg(InitLogLevel logLevel, const char *tag, const char *info)
Z
zhong_ning 已提交
74
{
X
fix log  
xionglei6 已提交
75 76
    static const char *LOG_LEVEL_STR[] = { "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" };
    static const char *LOG_KLEVEL_STR[] = { "<7>", "<6>", "<4>", "<3>", "<3>" };
S
sun_fan 已提交
77

Z
zhong_ning 已提交
78 79 80 81 82 83
    if (UNLIKELY(g_fd < 0)) {
        OpenLogDevice();
        if (g_fd < 0) {
            return;
        }
    }
M
Mupceet 已提交
84 85 86 87 88
    char logInfo[DEF_LOG_SIZE + DEF_LOG_SIZE] = {0};
    if (snprintf_s(logInfo, sizeof(logInfo), sizeof(logInfo) - 1, "%s[pid=%d][%s][%s]%s",
        LOG_KLEVEL_STR[logLevel], getpid(), tag, LOG_LEVEL_STR[logLevel], info) == -1) {
        logInfo[sizeof(logInfo) - 2] = '\n'; // 2 add \n to tail
        logInfo[sizeof(logInfo) - 1] = '\0';
Z
zhong_ning 已提交
89 90
        return;
    }
Z
zhong_ning 已提交
91
    if (write(g_fd, logInfo, strlen(logInfo)) < 0) {
M
Mupceet 已提交
92
        printf("%s\n", logInfo);
Z
zhong_ning 已提交
93 94 95
    }
    return;
}
X
fix log  
xionglei6 已提交
96
#endif
X
xionglei6 已提交
97

M
Mupceet 已提交
98 99 100
static void PrintLog(InitLogLevel logLevel, unsigned int domain, const char *tag, const char *logInfo)
{
#ifdef OHOS_LITE
M
Mupceet 已提交
101
#ifdef __LITEOS_M__
N
Neil Chen 已提交
102 103
    (void)logLevel;
    (void)domain;
M
Mupceet 已提交
104 105
    printf("[%s]%s \n", tag, logInfo);
#else
M
Mupceet 已提交
106 107 108
    static const LogLevel LOG_LEVEL[] = { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
    (void)HiLogPrint(INIT_LOG_INIT, LOG_LEVEL[logLevel], domain, tag, "%{public}s", logInfo);
#endif
M
Mupceet 已提交
109
#endif
M
Mupceet 已提交
110 111 112 113 114 115 116 117 118 119 120 121
#ifdef INIT_DMESG
    LogToDmesg(logLevel, tag, logInfo);
#endif
#ifdef INIT_AGENT
    static const LogLevel LOG_LEVEL[] = { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
    HiLogBasePrint(LOG_CORE, LOG_LEVEL[logLevel], domain, tag, "%{public}s", logInfo);
#endif
#ifdef INIT_FILE
    LogToFile(INIT_LOG_PATH"begetctl.log", tag, logInfo);
#endif
}

M
Mupceet 已提交
122
INIT_LOCAL_API void InitLog(int logLevel, unsigned int domain, const char *tag, const char *fmt, va_list vargs)
S
sun_fan 已提交
123
{
C
chengjinsong 已提交
124
    if ((int)g_logLevel > logLevel) {
S
sun_fan 已提交
125 126
        return;
    }
M
Mupceet 已提交
127 128
    char tmpFmt[DEF_LOG_SIZE] = {0};
    if (vsnprintf_s(tmpFmt, sizeof(tmpFmt), sizeof(tmpFmt) - 1, fmt, vargs) == -1) {
M
Mupceet 已提交
129 130
        tmpFmt[sizeof(tmpFmt) - 2] = '\n'; // 2 add \n to tail
        tmpFmt[sizeof(tmpFmt) - 1] = '\0';
S
sun_fan 已提交
131
    }
王liangliang's avatar
王liangliang 已提交
132
    PrintLog((InitLogLevel)logLevel, domain, tag, tmpFmt);
S
sun_fan 已提交
133
}
M
Mupceet 已提交
134

C
cheng_jinsong 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
INIT_PUBLIC_API void SetInitLogLevel(InitLogLevel level)
{
    if ((level >= INIT_DEBUG) && (level <= INIT_FATAL)) {
        g_logLevel = level;
    }
    return;
}

INIT_PUBLIC_API  int GetInitLogLevel(void)
{
    return g_logLevel;
}

C
cheng_jinsong 已提交
148
INIT_PUBLIC_API void EnableInitLog(InitLogLevel level)
M
Mupceet 已提交
149
{
C
cheng_jinsong 已提交
150
    g_logLevel = level;
M
Mupceet 已提交
151 152
    SetInitCommLog(InitLog);
}
C
cheng_jinsong 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

INIT_PUBLIC_API void EnableInitLogFromCmdline(void)
{
    SetInitCommLog(InitLog);
    char level[MAX_BUFFER_LEN] = {0};
    char *buffer = ReadFileData("/proc/cmdline");
    if (buffer == NULL) {
        INIT_LOGE("Failed to read \"/proc/cmdline\"");
        return;
    }
    int ret = GetProcCmdlineValue("initloglevel", buffer, level, MAX_BUFFER_LEN);
    free(buffer);
    if (ret != 0) {
        INIT_LOGE("Failed get log level from cmdline");
        return;
    }
    errno = 0;
    unsigned int logLevel = (unsigned int)strtoul(level, 0, 10); // 10 is decimal
    INIT_INFO_CHECK(errno == 0, return, "Failed strtoul %s, err=%d", level, errno);
    SetInitLogLevel(logLevel);
    return;
}