-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathlogger.cpp
More file actions
170 lines (143 loc) · 4.86 KB
/
logger.cpp
File metadata and controls
170 lines (143 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* 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 "logger.h"
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <cstdlib>
#include <ctime>
#ifdef USE_RDK_LOGGER
#include "rdk_debug.h"
#endif
namespace TTS {
static inline void sync_stdout()
{
if (getenv("SYNC_STDOUT"))
setvbuf(stdout, NULL, _IOLBF, 0);
}
std::string methodName(const std::string& prettyFunction)
{
size_t colons = prettyFunction.find("::");
size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
size_t end = prettyFunction.rfind("(") - begin;
return prettyFunction.substr(begin,end);
}
#ifdef USE_RDK_LOGGER
void logger_init()
{
sync_stdout();
TTS_logger_init("/etc/debug.ini");
}
void log(LogLevel level,
const char* func,
const char* file,
int line,
int, // thread id is already handled by TTS_logger
const char* format, ...)
{
const TTS_LogLevel levelMap[] =
{TTS_LOG_FATAL, TTS_LOG_ERROR, TTS_LOG_WARN, TTS_LOG_INFO, TTS_LOG_DEBUG, TTS_LOG_TRACE1};
const short kFormatMessageSize = 4096;
// TTSLogger is backed with log4c which has its own default level
// for filtering messages. Therefore, we don't check level here.
char userFormatted[kFormatMessageSize];
char finalFormatted[kFormatMessageSize];
va_list argptr;
va_start(argptr, format);
vsnprintf(userFormatted, kFormatMessageSize, format, argptr);
snprintf(finalFormatted, kFormatMessageSize, "%s:%s:%d %s", func,
basename(file),
line,
userFormatted);
va_end(argptr);
// Currently, we use customized layout 'comcast_dated_nocr' in log4c.
// This layout doesn't have trailing carriage return, so we need
// to add it explicitly.
// Once the default layout is used, this addition should be deleted.
RDK_LOG(levelMap[static_cast<int>(level)],
"LOG.RDK.TTS",
"%s\n",
finalFormatted);
if (FATAL_LEVEL == level)
std::abort();
}
#else
static int gDefaultLogLevel = ERROR_LEVEL;
// static int gDefaultLogLevel = VERBOSE_LEVEL;
void logger_init()
{
sync_stdout();
const char* level = getenv("TTS_DEFAULT_LOG_LEVEL");
if (level)
gDefaultLogLevel = static_cast<LogLevel>(atoi(level));
}
void log(LogLevel level,
const char* func,
const char* file,
int line,
int threadID,
const char* format, ...)
{
if (gDefaultLogLevel < level)
return;
const char* levelMap[] = {"Fatal", "Error", "Warning", "Info", "Verbose", "Trace"};
const short kFormatMessageSize = 4096;
char formatted[kFormatMessageSize];
va_list argptr;
va_start(argptr, format);
vsnprintf(formatted, kFormatMessageSize, format, argptr);
va_end(argptr);
char timestamp[0xFF] = {0};
struct timespec spec;
struct tm tm;
clock_gettime(CLOCK_REALTIME, &spec);
gmtime_r(&spec.tv_sec, &tm);
long ms = spec.tv_nsec / 1.0e6;
snprintf(timestamp, sizeof(timestamp), "%02d%02d%02d-%02d:%02d:%02d.%03ld",
tm.tm_year % 100,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
ms);
if (threadID)
{
printf("%s [%s] [pid=%d,tid=%d] %s:%s:%d %s\n",
timestamp,
levelMap[static_cast<int>(level)],
getpid(),
threadID,
func, basename(file), line,
formatted);
}
else
{
printf("%s [%s] %s:%s:%d %s\n",
timestamp,
levelMap[static_cast<int>(level)],
func, basename(file), line,
formatted);
}
fflush(stdout);
if (FATAL_LEVEL == level)
std::abort();
}
#endif // USE_TTS_LOGGER
} // namespace TTS