Skip to content

Commit

Permalink
chore(logger): combine Logger & formatter into TLogger
Browse files Browse the repository at this point in the history
* include/logger.h:
    * Add structure TLogger
    * Add macros TLOGGER_DEFAULT & TLOGGER_DEFAULT_LVSKIP()
      for the TLogger default object literals

* include/dao.h & lib/logger.c:
    * Add the default formatter logger_tag_formatter_default()
    * logger_tag():
        * Combine the formerly 1th param `logger` & the 4th param `formatter`
          into the new 1th parameter `tlogger`
        * Use the default formatter if `tlogger->formatter` is NULL

        * lib/shm.c: attach_err() & bshm_init()
        * maple/bbsd.c: blog_pid()
        * maple/xchatd.c: logtalk() & logit()

* include/dao.h & lib/shm.c
    * Replace shm_logger_init() & shm_formatter_init() with shm_tlogger_init()
        * maple/bbsd.c: main()
        * maple/xchatd.c: print_user_counts() (commented out)

* Rewrite loggers with formatters into TLogger objects
    * lib/shm.c: shm_logger & shm_formatter -> shm_tlogger
    * maple/bbsd.c: blog_logger & blog_formatter -> blog_tlogger
  • Loading branch information
IepIweidieng committed May 19, 2021
1 parent c05c5c9 commit 0eac429
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
5 changes: 2 additions & 3 deletions include/dao.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ void archiv32(time_t chrono, char *fname);
void archiv32m(time_t chrono, char *fname);
GCC_PURE time_t chrono32(const char *str);
/* shm.c */
void shm_logger_init(const Logger *logger);
void shm_formatter_init(void (*formatter)(char *buf, size_t size, const char *mode, const char *msg));
void shm_tlogger_init(const TLogger *tlogger);
GCC_NONNULLS void attach_err(int shmkey, const char *name);
void *attach_shm(int shmkey, int shmsize);
void *attach_shm_noinit(int shmkey, int shmsize);
Expand Down Expand Up @@ -205,7 +204,7 @@ int proc_runv_bg(const char *path, const char *argv[]);
GCC_SENTINEL(0) int proc_runl_bg(const char *path, const char *arg0, ...);
/* logger.c */
GCC_FORMAT(3, 4) GCC_NONNULL(1, 3) void loggerf(const Logger *logger, enum LogLevel level, const char *format, ...);
GCC_NONNULLS void logger_tag(const Logger *logger, const char *tag, const char *msg, void (*formatter)(char *buf, size_t size, const char *mode, const char *msg));
GCC_NONNULLS void logger_tag(const TLogger *tlogger, const char *tag, const char *msg);
/* xwrite.c */
int xwrite(int fd, const char *data, int size);

Expand Down
18 changes: 18 additions & 0 deletions include/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@ typedef struct {
enum LogLevel lv_skip; // The minimum log level to ignore
} Logger;

/* Tag Logger: A logger with tag and custom formatter */
typedef struct {
Logger logger;
void (*formatter)(char *buf, size_t size, const char *tag, const char *msg);
} TLogger;

#define TLOGGER_DEFAULT_LVSKIP(_lv_skip) \
LISTLIT(TLogger){ \
.logger = { \
.file = NULL, \
.path = NULL, /* Use the default log file (`stderr`) */ \
.lv_skip = (_lv_skip), \
}, \
.formatter = NULL, /* Use the default formatter */ \
}

#define TLOGGER_DEFAULT TLOGGER_DEFAULT_LVSKIP(LOGLV_WARN)

#endif // LOGGER_H
21 changes: 16 additions & 5 deletions lib/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,35 @@ void loggerf(const Logger *logger, enum LogLevel level, const char *format, ...)
fclose(file);
}

/* Log a message with tag and custem formatter to the file specified by `logger`
* Use `logger->file` if it is not `NULL`,
* otherwise temporarily open and then close the file with path `logger->path` if it is not `NULL`,
/* The default formatter for message logging with tag */
static void logger_tag_formatter_default(char *buf, size_t size, const char *tag, const char *msg)
{
snprintf(buf, size, "[%s] %s", tag, msg);
}

/* Log a message with tag and custem formatter to the file specified by `tlogger`
* Use `tlogger->logger->file` if it is not `NULL`,
* otherwise temporarily open and then close the file with path `tlogger->logger->path` if it is not `NULL`,
* otherwise output the message to `stderr`.
* If `tlogger->formatter` is `NULL`, the default formatter is used.
* All messages are not ignored.
* An extra newline will be appended to the message for output,
* therefore it is not needed to add a trailing newline in `formatter`. */
* therefore it is not needed to add a trailing newline in the formatter. */
GCC_NONNULLS
void logger_tag(const Logger *logger, const char *tag, const char *msg, void (*formatter)(char *buf, size_t size, const char *mode, const char *msg))
void logger_tag(const TLogger *tlogger, const char *tag, const char *msg)
{
const Logger *const logger = &tlogger->logger;
FILE *const file = logger->file ? logger->file : logger->path ? fopen(logger->path, "a") : stderr;

if (!file)
return;

void (*const formatter)(char *buf, size_t size, const char *mode, const char *msg) =
tlogger->formatter ? tlogger->formatter : logger_tag_formatter_default;
const int fd = fileno(file);

char buf[512];

formatter(buf, sizeof(buf) - 1, tag, msg); // Reserve 1 byte for the trailing newline
strcat(buf, "\n");
write(fd, buf, strlen(buf));
Expand Down
38 changes: 6 additions & 32 deletions lib/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,13 @@
#include "struct.h"
#include "dao.h"

/* Log output and formatting */
/* Tag logger */

static Logger shm_logger = {
.file = NULL,
.path = NULL,
.lv_skip = LOGLV_WARN,
};
static TLogger shm_tlogger = TLOGGER_DEFAULT;

static void shm_formatter_default(char *buf, size_t size, const char *tag, const char *msg)
void shm_tlogger_init(const TLogger *tlogger)
{
snprintf(buf, size, "[%s] %s", tag, msg);
}

static void (*shm_formatter)(char *buf, size_t size, const char *tag, const char *msg) = shm_formatter_default;

/* Setters */

/* Set `shm_logger` to `*logger`
* If `logger` is `NULL`, the default value is used */
void shm_logger_init(const Logger *logger)
{
shm_logger = (logger) ? *logger : LISTLIT(Logger){
.file = NULL,
.path = NULL,
.lv_skip = LOGLV_WARN,
};
}

/* Set `shm_formatter` to `formatter`
* If `formatter` is `NULL`, the default value is used */
void shm_formatter_init(void (*formatter)(char *buf, size_t size, const char *mode, const char *msg))
{
shm_formatter = (formatter) ? formatter : shm_formatter_default;
shm_tlogger = (tlogger) ? *tlogger : TLOGGER_DEFAULT;
}

/* SHM loader */
Expand All @@ -70,7 +44,7 @@ void attach_err(int shmkey, const char *name)

sprintf(tag, "%s error", name);
sprintf(msg, "key = %lx", (unsigned long)shmkey);
logger_tag(&shm_logger, tag, msg, shm_formatter);
logger_tag(&shm_tlogger, tag, msg);
exit(1);
}

Expand Down Expand Up @@ -186,7 +160,7 @@ void bshm_init(BCACHE **p_bshm)
/* 等所有 boards 資料更新後再設定 uptime */

time32(uptime);
logger_tag(&shm_logger, "CACHE", "reload bcache", shm_formatter);
logger_tag(&shm_tlogger, "CACHE", "reload bcache");
return;
}
}
Expand Down
20 changes: 11 additions & 9 deletions maple/bbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ int treat=0;
/* 離開 BBS 程式 */
/* ----------------------------------------------------- */

static Logger blog_logger = {
.file = NULL,
.path = FN_USIES,
.lv_skip = LOGLV_WARN,
};

static void blog_formatter(char *buf, size_t len, const char *mode, const char *msg)
{
snprintf(buf, len, "%s %-5.5s %-*s %s", Etime(&TEMPLVAL(time_t, {time(NULL)})), mode, IDLEN, cuser.userid, msg);
}

static TLogger blog_tlogger = {
.logger = {
.file = NULL,
.path = FN_USIES,
.lv_skip = LOGLV_WARN,
},
.formatter = blog_formatter,
};

void
blog_pid(
const char *mode,
Expand All @@ -88,7 +91,7 @@ blog_pid(
msg = data;
sprintf(data, "Stay: %d (%d)", (int)(time(NULL) - ap_start) / 60, pid);
}
logger_tag(&blog_logger, mode, msg, blog_formatter);
logger_tag(&blog_tlogger, mode, msg);
}

void
Expand Down Expand Up @@ -1909,8 +1912,7 @@ int main(int argc, char *argv[])
/* attach shared memory & semaphore */
/* --------------------------------------------------- */

shm_logger_init(&blog_logger);
shm_formatter_init(blog_formatter);
shm_tlogger_init(&blog_tlogger);

#ifdef HAVE_SEM
sem_init();
Expand Down
7 changes: 3 additions & 4 deletions maple/xchatd.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ logtalk(
const char *key,
const char *msg)
{
logger_tag(&TEMPLVAL(Logger, {.file = ftalk}), key, msg, log_formatter);
logger_tag(&TEMPLVAL(TLogger, {.logger = {.file = ftalk}, .formatter = log_formatter}), key, msg);
}


Expand All @@ -205,7 +205,7 @@ logit(
const char *key,
const char *msg)
{
logger_tag(&TEMPLVAL(Logger, {.file = flog}), key, msg, log_formatter);
logger_tag(&TEMPLVAL(TLogger, {.logger = {.file = flog}, .formatter = log_formatter}), key, msg);
}


Expand Down Expand Up @@ -1782,8 +1782,7 @@ print_user_counts(
sprintf(buf + strlen(buf), " [ %d 人在秘密包廂]", suserc);
send_to_user(cuser, buf, 0, number);

// shm_logger_init(&TEMPLVAL(Logger, {.file = flog}));
// shm_formatter_init(log_formatter);
// shm_logger_init(&TEMPLVAL(TLogger, {.logger = {.file = flog}, .formatter = log_formatter}));
// load_mud_like();
}

Expand Down

0 comments on commit 0eac429

Please sign in to comment.