10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "shell_lk.h"
330d163575Sopenharmony_ci#include "securec.h"
340d163575Sopenharmony_ci#include "stdio.h"
350d163575Sopenharmony_ci#include "stdlib.h"
360d163575Sopenharmony_ci#include "unistd.h"
370d163575Sopenharmony_ci#include "shcmd.h"
380d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG
390d163575Sopenharmony_ci#include "dmesg_pri.h"
400d163575Sopenharmony_ci#endif
410d163575Sopenharmony_ci#include "los_init.h"
420d163575Sopenharmony_ci#include "los_printf_pri.h"
430d163575Sopenharmony_ci#include "los_process_pri.h"
440d163575Sopenharmony_ci
450d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_LK
460d163575Sopenharmony_ci
470d163575Sopenharmony_citypedef enum {
480d163575Sopenharmony_ci    MODULE0 = 0,
490d163575Sopenharmony_ci    MODULE1 = 1,
500d163575Sopenharmony_ci    MODULE2 = 2,
510d163575Sopenharmony_ci    MODULE3 = 3,
520d163575Sopenharmony_ci    MODULE4 = 4,
530d163575Sopenharmony_ci} MODULE_FLAG;
540d163575Sopenharmony_ci
550d163575Sopenharmony_citypedef struct {
560d163575Sopenharmony_ci    INT32 module_level;
570d163575Sopenharmony_ci    INT32 trace_level;
580d163575Sopenharmony_ci    FILE *fp;
590d163575Sopenharmony_ci} Logger;
600d163575Sopenharmony_ci
610d163575Sopenharmony_ciSTATIC INT32 g_tracelevel;
620d163575Sopenharmony_ciSTATIC INT32 g_modulelevel;
630d163575Sopenharmony_ci
640d163575Sopenharmony_ciSTATIC Logger g_logger = { 0 };
650d163575Sopenharmony_ci
660d163575Sopenharmony_ciVOID OsLkDefaultFunc(INT32 level, const CHAR *func, INT32 line, const CHAR *fmt, va_list ap);
670d163575Sopenharmony_ci
680d163575Sopenharmony_ciLK_FUNC g_osLkHook = (LK_FUNC)OsLkDefaultFunc;
690d163575Sopenharmony_ci
700d163575Sopenharmony_ciSTATIC INLINE INT32 OsLkTraceLvGet(VOID)
710d163575Sopenharmony_ci{
720d163575Sopenharmony_ci    return g_tracelevel;
730d163575Sopenharmony_ci}
740d163575Sopenharmony_ci
750d163575Sopenharmony_ciconst CHAR *OsLkCurLogLvGet(VOID)
760d163575Sopenharmony_ci{
770d163575Sopenharmony_ci    return OsLogLvGet(g_tracelevel);
780d163575Sopenharmony_ci}
790d163575Sopenharmony_ci
800d163575Sopenharmony_ciVOID OsLkTraceLvSet(INT32 level)
810d163575Sopenharmony_ci{
820d163575Sopenharmony_ci    g_tracelevel = level;
830d163575Sopenharmony_ci    g_logger.trace_level = level;
840d163575Sopenharmony_ci    return;
850d163575Sopenharmony_ci}
860d163575Sopenharmony_ci
870d163575Sopenharmony_ciVOID OsLkModuleLvSet(INT32 level)
880d163575Sopenharmony_ci{
890d163575Sopenharmony_ci    g_modulelevel = level;
900d163575Sopenharmony_ci    g_logger.module_level = level;
910d163575Sopenharmony_ci    return;
920d163575Sopenharmony_ci}
930d163575Sopenharmony_ci
940d163575Sopenharmony_ciINT32 OsLkModuleLvGet(VOID)
950d163575Sopenharmony_ci{
960d163575Sopenharmony_ci    return g_modulelevel;
970d163575Sopenharmony_ci}
980d163575Sopenharmony_ci
990d163575Sopenharmony_ciVOID OsLkLogFileSet(const CHAR *str)
1000d163575Sopenharmony_ci{
1010d163575Sopenharmony_ci    FILE *fp = NULL;
1020d163575Sopenharmony_ci    FILE *oldfp = g_logger.fp;
1030d163575Sopenharmony_ci
1040d163575Sopenharmony_ci    if (str == NULL) {
1050d163575Sopenharmony_ci        return;
1060d163575Sopenharmony_ci    }
1070d163575Sopenharmony_ci    fp = fopen(str, "w+");
1080d163575Sopenharmony_ci    if (fp == NULL) {
1090d163575Sopenharmony_ci        printf("Error can't open the %s file\n", str);
1100d163575Sopenharmony_ci        return;
1110d163575Sopenharmony_ci    }
1120d163575Sopenharmony_ci
1130d163575Sopenharmony_ci    g_logger.fp = fp;
1140d163575Sopenharmony_ci    if (oldfp != NULL) {
1150d163575Sopenharmony_ci        fclose(oldfp);
1160d163575Sopenharmony_ci    }
1170d163575Sopenharmony_ci}
1180d163575Sopenharmony_ci
1190d163575Sopenharmony_ciFILE *OsLogFpGet(VOID)
1200d163575Sopenharmony_ci{
1210d163575Sopenharmony_ci    return g_logger.fp;
1220d163575Sopenharmony_ci}
1230d163575Sopenharmony_ci
1240d163575Sopenharmony_ciINT32 CmdLog(INT32 argc, const CHAR **argv)
1250d163575Sopenharmony_ci{
1260d163575Sopenharmony_ci    size_t level;
1270d163575Sopenharmony_ci    size_t module;
1280d163575Sopenharmony_ci    CHAR *p = NULL;
1290d163575Sopenharmony_ci
1300d163575Sopenharmony_ci    if ((argc != 2) || (argv == NULL)) { /* 2:count of parameter */
1310d163575Sopenharmony_ci        PRINTK("Usage: log level <num>\n");
1320d163575Sopenharmony_ci        PRINTK("Usage: log module <num>\n");
1330d163575Sopenharmony_ci        PRINTK("Usage: log path <PATH>\n");
1340d163575Sopenharmony_ci        return -1;
1350d163575Sopenharmony_ci    }
1360d163575Sopenharmony_ci
1370d163575Sopenharmony_ci    if (!strncmp(argv[0], "level", strlen(argv[0]) + 1)) {
1380d163575Sopenharmony_ci        level = strtoul(argv[1], &p, 0);
1390d163575Sopenharmony_ci        if ((*p != 0) || (level > LOS_TRACE_LEVEL) || (level < LOS_EMG_LEVEL)) {
1400d163575Sopenharmony_ci            PRINTK("current log level %s\n", OsLkCurLogLvGet());
1410d163575Sopenharmony_ci            PRINTK("log %s [num] can access as 0:EMG 1:COMMON 2:ERROR 3:WARN 4:INFO 5:DEBUG\n", argv[0]);
1420d163575Sopenharmony_ci        } else {
1430d163575Sopenharmony_ci            OsLkTraceLvSet(level);
1440d163575Sopenharmony_ci            PRINTK("Set current log level %s\n", OsLkCurLogLvGet());
1450d163575Sopenharmony_ci        }
1460d163575Sopenharmony_ci    } else if (!strncmp(argv[0], "module", strlen(argv[0]) + 1)) {
1470d163575Sopenharmony_ci        module = strtoul(argv[1], &p, 0);
1480d163575Sopenharmony_ci        if ((*p != 0) || (module > MODULE4)) {
1490d163575Sopenharmony_ci            PRINTK("log %s can't access %s\n", argv[0], argv[1]);
1500d163575Sopenharmony_ci            PRINTK("not support yet\n");
1510d163575Sopenharmony_ci            return -1;
1520d163575Sopenharmony_ci        } else {
1530d163575Sopenharmony_ci            OsLkModuleLvSet(module);
1540d163575Sopenharmony_ci            PRINTK("not support yet\n");
1550d163575Sopenharmony_ci        }
1560d163575Sopenharmony_ci    } else if (!strncmp(argv[0], "path", strlen(argv[0]) + 1)) {
1570d163575Sopenharmony_ci        PRINTK("not support yet\n");
1580d163575Sopenharmony_ci    } else {
1590d163575Sopenharmony_ci        PRINTK("Usage: log level <num>\n");
1600d163575Sopenharmony_ci        PRINTK("Usage: log module <num>\n");
1610d163575Sopenharmony_ci        PRINTK("Usage: log path <PATH>\n");
1620d163575Sopenharmony_ci        return -1;
1630d163575Sopenharmony_ci    }
1640d163575Sopenharmony_ci
1650d163575Sopenharmony_ci    return 0;
1660d163575Sopenharmony_ci}
1670d163575Sopenharmony_ci
1680d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG
1690d163575Sopenharmony_ciSTATIC INLINE VOID OsLogCycleRecord(INT32 level)
1700d163575Sopenharmony_ci{
1710d163575Sopenharmony_ci    UINT32 tmpLen;
1720d163575Sopenharmony_ci    if (level != LOS_COMMON_LEVEL && (level > LOS_EMG_LEVEL && level <= LOS_TRACE_LEVEL)) {
1730d163575Sopenharmony_ci        tmpLen = strlen(OsLogLvGet(level));
1740d163575Sopenharmony_ci        const CHAR* tmpPtr = OsLogLvGet(level);
1750d163575Sopenharmony_ci        (VOID)OsLogRecordStr(tmpPtr, tmpLen);
1760d163575Sopenharmony_ci    }
1770d163575Sopenharmony_ci}
1780d163575Sopenharmony_ci#endif
1790d163575Sopenharmony_ci
1800d163575Sopenharmony_ciVOID OsLkDefaultFunc(INT32 level, const CHAR *func, INT32 line, const CHAR *fmt, va_list ap)
1810d163575Sopenharmony_ci{
1820d163575Sopenharmony_ci    if (level > OsLkTraceLvGet()) {
1830d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG
1840d163575Sopenharmony_ci        if ((UINT32)level <= OsDmesgLvGet()) {
1850d163575Sopenharmony_ci            OsLogCycleRecord(level);
1860d163575Sopenharmony_ci            DmesgPrintf(fmt, ap);
1870d163575Sopenharmony_ci        }
1880d163575Sopenharmony_ci#endif
1890d163575Sopenharmony_ci        return;
1900d163575Sopenharmony_ci    }
1910d163575Sopenharmony_ci    if ((level != LOS_COMMON_LEVEL) && ((level > LOS_EMG_LEVEL) && (level <= LOS_TRACE_LEVEL))) {
1920d163575Sopenharmony_ci        dprintf("[%s][%s:%s]", OsLogLvGet(level),
1930d163575Sopenharmony_ci                ((OsCurrProcessGet() == NULL) ? "NULL" : OsCurrProcessGet()->processName),
1940d163575Sopenharmony_ci                ((OsCurrTaskGet() == NULL) ? "NULL" : OsCurrTaskGet()->taskName));
1950d163575Sopenharmony_ci    }
1960d163575Sopenharmony_ci    LkDprintf(fmt, ap);
1970d163575Sopenharmony_ci}
1980d163575Sopenharmony_ci
1990d163575Sopenharmony_ciVOID LOS_LkPrint(INT32 level, const CHAR *func, INT32 line, const CHAR *fmt, ...)
2000d163575Sopenharmony_ci{
2010d163575Sopenharmony_ci    va_list ap;
2020d163575Sopenharmony_ci    if (g_osLkHook != NULL) {
2030d163575Sopenharmony_ci        va_start(ap, fmt);
2040d163575Sopenharmony_ci        g_osLkHook(level, func, line, fmt, ap);
2050d163575Sopenharmony_ci        va_end(ap);
2060d163575Sopenharmony_ci    }
2070d163575Sopenharmony_ci}
2080d163575Sopenharmony_ci
2090d163575Sopenharmony_ciVOID LOS_LkRegHook(LK_FUNC hook)
2100d163575Sopenharmony_ci{
2110d163575Sopenharmony_ci    g_osLkHook = hook;
2120d163575Sopenharmony_ci}
2130d163575Sopenharmony_ci
2140d163575Sopenharmony_ciUINT32 OsLkLoggerInit(VOID)
2150d163575Sopenharmony_ci{
2160d163575Sopenharmony_ci    (VOID)memset_s(&g_logger, sizeof(Logger), 0, sizeof(Logger));
2170d163575Sopenharmony_ci    OsLkTraceLvSet(TRACE_DEFAULT);
2180d163575Sopenharmony_ci    LOS_LkRegHook(OsLkDefaultFunc);
2190d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG
2200d163575Sopenharmony_ci    (VOID)LOS_DmesgLvSet(TRACE_DEFAULT);
2210d163575Sopenharmony_ci#endif
2220d163575Sopenharmony_ci    return LOS_OK;
2230d163575Sopenharmony_ci}
2240d163575Sopenharmony_ci
2250d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_CMD_DEBUG
2260d163575Sopenharmony_ciSHELLCMD_ENTRY(log_shellcmd, CMD_TYPE_EX, "log", 1, (CmdCallBackFunc)CmdLog);
2270d163575Sopenharmony_ci#endif
2280d163575Sopenharmony_ci
2290d163575Sopenharmony_ciLOS_MODULE_INIT(OsLkLoggerInit, LOS_INIT_LEVEL_EARLIEST);
2300d163575Sopenharmony_ci
2310d163575Sopenharmony_ci#endif
232