1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <time.h>
10 #include "ulog/ulog.h"
11 #include "ulog_api.h"
12 #include "aos/kernel.h"
13 #include "aos/errno.h"
14 #include "ulog_ring_fifo.h"
15 
16 #if SYSLOG_TIME_FORMAT
17 static const char months[][4] = { "Jan", "Feb", "Mar", "Apr", "May",
18                                   "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
19                                 };
20 #endif
21 
22 static aos_mutex_t log_mutex;
23 
24 /* log init mutex */
log_init_mutex(void)25 void log_init_mutex(void)
26 {
27     aos_mutex_new(&log_mutex);
28 }
29 
log_get_mutex()30 bool log_get_mutex()
31 {
32     return 0 == aos_mutex_lock(&log_mutex, AOS_WAIT_FOREVER);
33 }
34 
log_release_mutex()35 void log_release_mutex()
36 {
37     aos_mutex_unlock(&log_mutex);
38 }
39 
40 /* result like 99.356 ,i.e. s.ms */
41 /* Result is like "Nov 28 15:19:20.122" */
ulog_format_time(char * buffer,const int len)42 char *ulog_format_time(char *buffer, const int len)
43 {
44     if(NULL!=buffer && len>4) {
45         long long ms = aos_now_ms();
46 #if SYSLOG_TIME_FORMAT
47         time_t rawtime;
48         time(&rawtime);
49         struct tm *tm = localtime(&rawtime);
50         /* %b format of strftime() is platform-dependent, so we realized it by-self */
51         snprintf(buffer, len, "%s ", months[tm->tm_mon < 12 ? tm->tm_mon : 0]);
52         strftime(&buffer[4], len - 4, "%d %H:%M:%S", tm);
53         const int32_t milli = ms % 1000;
54         char      ms_str[8] = "";
55         memset(ms_str, 0, sizeof(ms_str));
56         snprintf(ms_str, sizeof(ms_str), ".%03d", milli);
57         strncat(buffer, ms_str, len - strlen(buffer) - 1);
58 #else /* !SYSLOG_TIME_FORMAT */
59         snprintf(buffer, len, "%4d.%03d", (int)(ms / 1000), (int)(ms % 1000));
60 #endif /* SYSLOG_TIME_FORMAT */
61     }
62     return buffer;
63 }
64 
trim_file_path(const char * path)65 char* trim_file_path(const char* path)
66 {
67 #if SYNC_ABS_PATH
68     return path;
69 #else
70     char* filename = (char*)path;
71     if (path != NULL) {
72         if ((filename = strrchr(path, '/')) != NULL) {
73             filename++;
74         }
75     }
76 
77     if (filename != NULL) {
78         const size_t filename_len = strlen(filename);
79         if (filename_len > 0) {
80             if (filename_len > TAG_FILE_NAME_MAX_LEN) { /* trig it using last 32 string */
81                 filename += filename_len - TAG_FILE_NAME_MAX_LEN;
82             }
83         } else { /* cannot get file name if input path is like "/d/timer/" */
84             filename = UNDEFINE_FILE_NAME;
85         }
86     } else { /* using UNDEFINE_FILE_NAME */
87         filename = UNDEFINE_FILE_NAME;
88     }
89 
90     return filename;
91 #endif
92 }
93 
ulog_man(const char * cmd_str)94 int ulog_man(const char* cmd_str)
95 {
96 #if ULOG_CONFIG_ASYNC
97     int rc = -EINVAL;
98     if (NULL != cmd_str && strlen(cmd_str) > 0) {
99         rc = -EPERM;
100         if (aos_ulog_init) {
101             const uint16_t cmd_str_size = strlen(cmd_str) + strlen(ULOG_CMD_PREFIX) + 1;
102             char* tmpbuf = (char*)aos_malloc(cmd_str_size);
103             strncpy(tmpbuf, ULOG_CMD_PREFIX, cmd_str_size);
104             strncat(tmpbuf, cmd_str, cmd_str_size - strlen(ULOG_CMD_PREFIX) - 1);
105             rc = uring_fifo_push_s(tmpbuf, cmd_str_size);
106             aos_free(tmpbuf);
107         }
108     }
109 #else /*!ULOG_CONFIG_ASYNC */
110     int rc = -EACCES;
111 #endif
112     return rc;
113 }
114 
115