• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <fcntl.h>
17 #include <securec.h>
18 #include <unistd.h>
19 #include <time.h>
20 
21 #include "hilog_command.h"
22 
23 #define HILOG_DRIVER "/dev/hilog"
24 #define HILOG_LOGBUFFER 1024
25 
26 #undef LOG_TAG
27 #define LOG_TAG "hilogcat"
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[])
30 {
31     int ret;
32     bool printFlag = true;
33     if (argc > 1) {
34         ret = HilogCmdProc(LOG_TAG, argc, argv);
35         if (ret == -1 || g_hiviewConfig.silenceMod == SILENT_MODE_ON) {
36             return 0;
37         }
38     }
39     int fd = open(HILOG_DRIVER, O_RDONLY);
40     if (fd < 0) {
41         HILOG_ERROR(LOG_CORE, "open hilog driver failed\n");
42         return 0;
43     }
44     char *buf = malloc(HILOG_LOGBUFFER);
45     if (buf == NULL) {
46         close(fd);
47         return 0;
48     }
49     while (1) {
50         (void)memset_s(buf, HILOG_LOGBUFFER, 0, HILOG_LOGBUFFER);
51         ret = read(fd, buf, HILOG_LOGBUFFER);
52         if (ret < sizeof(struct HiLogEntry)) {
53             continue;
54         }
55         struct HiLogEntry *head = (struct HiLogEntry *)buf;
56 
57         time_t rawtime;
58         struct tm *info = NULL;
59         struct tm nowTime = {0};
60         unsigned int sec = head->sec;
61         rawtime = (time_t)sec;
62         /* Get local time */
63         info = localtime_r(&rawtime, &nowTime);
64 
65         printFlag = FilterLevelLog(g_hiviewConfig.level, *(head->msg));
66         if (!printFlag) {
67             continue;
68         }
69 #define MODULE_OFFSET 2
70         printFlag = FilterModuleLog(g_hiviewConfig.logOutputModule, (head->msg) + MODULE_OFFSET);
71         if (!printFlag) {
72             continue;
73         }
74         if (info == NULL) {
75             continue;
76         }
77         buf[HILOG_LOGBUFFER - 1] = '\0';
78         printf("%02d-%02d %02d:%02d:%02d.%03d %d %d %s\n", info->tm_mon + 1, info->tm_mday, info->tm_hour, info->tm_min,
79             info->tm_sec, head->nsec / NANOSEC_PER_MIRCOSEC, head->pid, head->taskId, head->msg);
80     }
81     free(buf);
82     close(fd);
83     return 0;
84 }
85