Skip to content

Commit 8c0cc4b

Browse files
committed
feat: Implement logging to pmlogd
1 parent 6c44b0e commit 8c0cc4b

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

service/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ set(BIN_NAME loader_service)
5252

5353
set(SRC_LIST
5454
${CMAKE_SOURCE_DIR}/src/main.c
55+
${CMAKE_SOURCE_DIR}/src/log.c
56+
${CMAKE_SOURCE_DIR}/src/utils.c
5557
)
5658

5759
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/dist/")
@@ -62,6 +64,8 @@ set(CMAKE_EXE_LINKER_FLAGS "-Wl,--allow-shlib-undefined")
6264
set_target_properties(${BIN_NAME} PROPERTIES LINKER_LANGUAGE C)
6365

6466
target_link_libraries (${BIN_NAME}
67+
rt
68+
pthread
6569
${GTHREAD2_LDFLAGS}
6670
${PBNJSON_LDFLAGS}
6771
${LS2_LDFLAGS}

service/src/log.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "log.h"
2+
#include "utils.h"
3+
#include <PmLogLib.h>
4+
#include <stdint.h>
5+
#include <time.h>
6+
7+
uint64_t start = 0;
8+
PmLogContext context;
9+
LogLevel current_log_level = Info;
10+
11+
void log_init()
12+
{
13+
PmLogGetContext("hyperion-webos", &context);
14+
}
15+
16+
void log_set_level(LogLevel level)
17+
{
18+
current_log_level = level;
19+
}
20+
21+
void log_printf(LogLevel level, const char* module, const char* fmt, ...)
22+
{
23+
va_list args;
24+
va_start(args, fmt);
25+
const char* level_str;
26+
const char* color_str;
27+
28+
if (start == 0)
29+
start = getticks_us();
30+
31+
if (level == Debug) {
32+
level_str = "DBG";
33+
color_str = "\x1b[34m";
34+
} else if (level == Info) {
35+
level_str = "INFO";
36+
color_str = "\x1b[36m";
37+
} else if (level == Warning) {
38+
level_str = "WARN";
39+
color_str = "\x1b[33m";
40+
} else {
41+
level_str = "ERR";
42+
color_str = "\x1b[31m";
43+
}
44+
45+
char formatted[1024];
46+
int prefix = snprintf(formatted, 1024, "%s: ", module);
47+
48+
if (prefix < 0 || prefix > 1024) {
49+
// This shall only happen if module is 1020 characters long.
50+
fprintf(stderr, "Log message truncated...\n");
51+
return;
52+
}
53+
54+
vsnprintf(formatted + prefix, 1024 - prefix, fmt, args);
55+
56+
_PmLogMsgKV(context, (PmLogLevel)level, 0, level == Debug ? NULL : level_str, 0, NULL, NULL, formatted);
57+
58+
if (level <= current_log_level) {
59+
fprintf(stderr, "%10.3fs %s[%4s %-20s]\x1b[0m %s\n", (getticks_us() - start) / 1000000.0, color_str, level_str, module, formatted + prefix);
60+
}
61+
62+
va_end(args);
63+
}

service/src/log.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
#include <stdarg.h>
3+
#include <stdio.h>
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
// Shorthand for PmLogLevel: https://github.com/openwebos/pmloglib/blob/master/include/public/PmLogLib.h.in#L163
10+
typedef enum {
11+
Error = 3,
12+
Warning = 4,
13+
Info = 6,
14+
Debug = 7,
15+
} LogLevel;
16+
17+
void log_init();
18+
void log_set_level(LogLevel level);
19+
void log_printf(LogLevel level, const char* module, const char* fmt, ...);
20+
21+
#define LOG(level, ...) log_printf(level, __func__, __VA_ARGS__)
22+
#define ERR(...) LOG(Error, __VA_ARGS__)
23+
#define WARN(...) LOG(Warning, __VA_ARGS__)
24+
#define INFO(...) LOG(Info, __VA_ARGS__)
25+
#define DBG(...) LOG(Debug, __VA_ARGS__)
26+
27+
#ifdef __cplusplus
28+
}
29+
#endif

service/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <luna-service2/lunaservice.h>
1111
#include <pbnjson.h>
1212
#include "service.h"
13+
#include "log.h"
1314

1415
GMainLoop *gmainLoop;
1516

@@ -156,7 +157,7 @@ int hyperiond_stop(service_t* service)
156157
ERR("hyperiond_stop: pthread_join failed, res=%d", res);
157158
return 4;
158159
}
159-
service->execution_thread = NULL;
160+
service->execution_thread = (pthread_t) NULL;
160161

161162
return 0;
162163
}
@@ -336,6 +337,7 @@ int main()
336337
service.daemon_pid = 0;
337338
service.hyperiond_version = NULL;
338339

340+
log_init();
339341
LSErrorInit(&lserror);
340342

341343
// create a GMainLoop

service/src/utils.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "utils.h"
2+
#include <time.h>
3+
4+
uint64_t getticks_us()
5+
{
6+
struct timespec tp;
7+
clock_gettime(CLOCK_MONOTONIC, &tp);
8+
return tp.tv_sec * 1000000 + tp.tv_nsec / 1000;
9+
}

service/src/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
uint64_t getticks_us();

0 commit comments

Comments
 (0)