2020-11-11 01:19:49 +03:00
|
|
|
/*
|
|
|
|
* MIT License
|
|
|
|
*
|
2021-02-05 20:46:59 +03:00
|
|
|
* Copyright (c) 2021 Ozan Tezcan
|
2020-11-11 01:19:49 +03:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-02-10 21:33:03 +03:00
|
|
|
#ifndef _XOPEN_SOURCE
|
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#endif
|
2021-02-05 16:35:10 +03:00
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
#include "sc_log.h"
|
|
|
|
|
2021-02-05 16:35:10 +03:00
|
|
|
#include <ctype.h>
|
2020-11-11 01:19:49 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2020-11-16 03:58:37 +03:00
|
|
|
#ifndef thread_local
|
|
|
|
#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
|
|
|
|
#define thread_local _Thread_local
|
|
|
|
#elif defined _WIN32 && (defined _MSC_VER || defined __ICL || \
|
|
|
|
defined __DMC__ || defined __BORLANDC__)
|
|
|
|
#define thread_local __declspec(thread)
|
|
|
|
#elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
|
|
|
|
#define thread_local __thread
|
|
|
|
#else
|
|
|
|
#error "Cannot define thread_local"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-02-09 19:12:15 +03:00
|
|
|
#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_ATOMIC__
|
|
|
|
#define SC_ATOMIC
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
|
|
|
#define sc_atomic _Atomic
|
|
|
|
#define sc_atomic_store(var, val) \
|
|
|
|
(atomic_store_explicit(var, val, memory_order_relaxed))
|
|
|
|
#define sc_atomic_load(var) \
|
|
|
|
(atomic_load_explicit(var, memory_order_relaxed))
|
|
|
|
#else
|
|
|
|
#define sc_atomic
|
|
|
|
#define sc_atomic_store(var, val) ((*(var)) = (val))
|
|
|
|
#define sc_atomic_load(var) (*(var))
|
|
|
|
#endif
|
2020-11-16 03:58:37 +03:00
|
|
|
|
2021-02-09 19:12:15 +03:00
|
|
|
thread_local char sc_name[32] = "Thread";
|
2021-02-06 21:36:18 +03:00
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
|
|
|
#pragma warning(disable : 4996)
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
struct sc_log_mutex
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION mtx;
|
|
|
|
};
|
|
|
|
|
|
|
|
int sc_log_mutex_init(struct sc_log_mutex *mtx)
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&mtx->mtx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sc_log_mutex_term(struct sc_log_mutex *mtx)
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&mtx->mtx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sc_log_mutex_lock(struct sc_log_mutex *mtx)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&mtx->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sc_log_mutex_unlock(struct sc_log_mutex *mtx)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&mtx->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
struct sc_log_mutex
|
|
|
|
{
|
|
|
|
pthread_mutex_t mtx;
|
|
|
|
};
|
|
|
|
|
|
|
|
int sc_log_mutex_init(struct sc_log_mutex *mtx)
|
|
|
|
{
|
2020-11-16 03:58:37 +03:00
|
|
|
int rc;
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
mtx->mtx = mut;
|
|
|
|
|
2020-11-16 03:58:37 +03:00
|
|
|
rc = pthread_mutexattr_init(&attr);
|
|
|
|
if (rc != 0) {
|
2021-02-06 21:36:18 +03:00
|
|
|
return rc;
|
2020-11-16 03:58:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
|
|
|
rc = pthread_mutex_init(&mtx->mtx, &attr);
|
2020-11-11 01:19:49 +03:00
|
|
|
pthread_mutexattr_destroy(&attr);
|
2020-11-29 23:21:25 +03:00
|
|
|
|
2021-02-06 21:36:18 +03:00
|
|
|
return rc;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 02:55:27 +03:00
|
|
|
void sc_log_mutex_term(struct sc_log_mutex *mtx)
|
2020-11-11 01:19:49 +03:00
|
|
|
{
|
2021-01-14 02:55:27 +03:00
|
|
|
pthread_mutex_destroy(&mtx->mtx);
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void sc_log_mutex_lock(struct sc_log_mutex *mtx)
|
|
|
|
{
|
2020-11-25 09:25:38 +03:00
|
|
|
pthread_mutex_lock(&mtx->mtx);
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void sc_log_mutex_unlock(struct sc_log_mutex *mtx)
|
|
|
|
{
|
2020-11-25 09:25:38 +03:00
|
|
|
pthread_mutex_unlock(&mtx->mtx);
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sc_log
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
const char *current_file;
|
|
|
|
const char *prev_file;
|
|
|
|
size_t file_size;
|
|
|
|
|
|
|
|
struct sc_log_mutex mtx;
|
2021-02-09 19:12:15 +03:00
|
|
|
sc_atomic enum sc_log_level level;
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
bool to_stdout;
|
2021-02-06 21:36:18 +03:00
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
void *arg;
|
2021-02-06 21:36:18 +03:00
|
|
|
int (*cb)(void *, enum sc_log_level, const char *, va_list);
|
2020-11-11 01:19:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sc_log sc_log;
|
|
|
|
|
|
|
|
int sc_log_init(void)
|
|
|
|
{
|
2020-11-29 23:21:25 +03:00
|
|
|
int rc;
|
|
|
|
|
2021-02-05 16:35:10 +03:00
|
|
|
sc_log = (struct sc_log){0};
|
2020-11-29 23:21:25 +03:00
|
|
|
|
2021-02-09 19:12:15 +03:00
|
|
|
sc_atomic_store(&sc_log.level, SC_LOG_INFO);
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_log.to_stdout = true;
|
|
|
|
|
2020-11-29 23:21:25 +03:00
|
|
|
rc = sc_log_mutex_init(&sc_log.mtx);
|
|
|
|
if (rc != 0) {
|
2021-02-06 21:36:18 +03:00
|
|
|
errno = rc;
|
2020-11-29 23:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int sc_log_term(void)
|
|
|
|
{
|
2021-01-14 02:55:27 +03:00
|
|
|
int rc = 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
if (sc_log.fp) {
|
2021-01-14 02:55:27 +03:00
|
|
|
rc = fclose(sc_log.fp);
|
|
|
|
if (rc != 0) {
|
2020-11-25 09:25:38 +03:00
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 02:55:27 +03:00
|
|
|
sc_log_mutex_term(&sc_log.mtx);
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-11-16 03:58:37 +03:00
|
|
|
void sc_log_set_thread_name(const char *name)
|
|
|
|
{
|
2020-12-27 16:14:34 +03:00
|
|
|
strncpy(sc_name, name, sizeof(sc_name) - 1);
|
2020-11-16 03:58:37 +03:00
|
|
|
}
|
|
|
|
|
2021-02-05 16:35:10 +03:00
|
|
|
static int sc_strcasecmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (;; a++, b++) {
|
|
|
|
if (*a == 0 && *b == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((n = tolower(*a) - tolower(*b)) != 0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
int sc_log_set_level(const char *str)
|
|
|
|
{
|
2021-02-09 19:12:15 +03:00
|
|
|
size_t count = sizeof(sc_log_levels) / sizeof(sc_log_levels[0]);
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
2021-02-05 16:35:10 +03:00
|
|
|
if (sc_strcasecmp(str, sc_log_levels[i].str) == 0) {
|
2021-02-09 19:12:15 +03:00
|
|
|
#ifdef SC_ATOMIC
|
|
|
|
sc_atomic_store(&sc_log.level, sc_log_levels[i].id);
|
|
|
|
#else
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_log_mutex_lock(&sc_log.mtx);
|
|
|
|
sc_log.level = sc_log_levels[i].id;
|
|
|
|
sc_log_mutex_unlock(&sc_log.mtx);
|
2021-02-09 19:12:15 +03:00
|
|
|
#endif
|
2020-11-11 01:19:49 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:36:18 +03:00
|
|
|
void sc_log_set_stdout(bool enable)
|
2020-11-11 01:19:49 +03:00
|
|
|
{
|
|
|
|
sc_log_mutex_lock(&sc_log.mtx);
|
|
|
|
sc_log.to_stdout = enable;
|
|
|
|
sc_log_mutex_unlock(&sc_log.mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sc_log_set_file(const char *prev_file, const char *current_file)
|
|
|
|
{
|
2021-02-06 21:36:18 +03:00
|
|
|
int rc = 0, saved_errno = 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
long size;
|
|
|
|
FILE *fp = NULL;
|
|
|
|
|
|
|
|
sc_log_mutex_lock(&sc_log.mtx);
|
|
|
|
|
|
|
|
if (sc_log.fp != NULL) {
|
2021-01-24 18:16:48 +03:00
|
|
|
fclose(sc_log.fp);
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_log.fp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_log.prev_file = prev_file;
|
|
|
|
sc_log.current_file = current_file;
|
|
|
|
|
|
|
|
if (prev_file == NULL || current_file == NULL) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(sc_log.current_file, "a+");
|
|
|
|
if (fp == NULL || fprintf(fp, "\n") < 0 || (size = ftell(fp)) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2021-01-24 17:56:18 +03:00
|
|
|
sc_log.file_size = (size_t) size;
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_log.fp = fp;
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
error:
|
|
|
|
rc = -1;
|
2021-02-06 21:36:18 +03:00
|
|
|
saved_errno = errno;
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
if (fp != NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
sc_log_mutex_unlock(&sc_log.mtx);
|
2021-02-06 21:36:18 +03:00
|
|
|
errno = saved_errno;
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:36:18 +03:00
|
|
|
void sc_log_set_callback(void *arg, int (*cb)(void *, enum sc_log_level,
|
|
|
|
const char *, va_list))
|
2020-11-11 01:19:49 +03:00
|
|
|
{
|
|
|
|
sc_log_mutex_lock(&sc_log.mtx);
|
|
|
|
sc_log.arg = arg;
|
2021-02-06 21:36:18 +03:00
|
|
|
sc_log.cb = cb;
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_log_mutex_unlock(&sc_log.mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sc_log_print_header(FILE *fp, enum sc_log_level level)
|
|
|
|
{
|
2020-11-29 23:21:25 +03:00
|
|
|
int rc;
|
2020-11-11 01:19:49 +03:00
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm *tm = localtime(&t);
|
|
|
|
|
|
|
|
if (tm == NULL) {
|
2021-02-06 21:36:18 +03:00
|
|
|
return -1;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:21:25 +03:00
|
|
|
rc = fprintf(fp, "[%d-%02d-%02d %02d:%02d:%02d][%-5s][%s] ",
|
|
|
|
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
|
|
|
|
tm->tm_min, tm->tm_sec, sc_log_levels[level].str, sc_name);
|
|
|
|
if (rc < 0) {
|
2021-02-06 21:36:18 +03:00
|
|
|
return -1;
|
2020-11-29 23:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sc_log_stdout(enum sc_log_level level, const char *fmt, va_list va)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = sc_log_print_header(stdout, level);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:21:25 +03:00
|
|
|
rc = vfprintf(stdout, fmt, va);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sc_log_file(enum sc_log_level level, const char *fmt, va_list va)
|
|
|
|
{
|
|
|
|
int rc, size;
|
|
|
|
|
|
|
|
rc = sc_log_print_header(sc_log.fp, level);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = vfprintf(sc_log.fp, fmt, va);
|
|
|
|
if (size < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_log.file_size += size;
|
|
|
|
|
|
|
|
if (sc_log.file_size > SC_LOG_FILE_SIZE) {
|
|
|
|
fclose(sc_log.fp);
|
|
|
|
(void) rename(sc_log.current_file, sc_log.prev_file);
|
|
|
|
|
|
|
|
sc_log.fp = fopen(sc_log.current_file, "w+");
|
|
|
|
if (sc_log.fp == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_log.file_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sc_log_log(enum sc_log_level level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
va_list va;
|
|
|
|
|
2021-02-09 19:12:15 +03:00
|
|
|
// Use relaxed atomics to avoid locking cost, e.g DEBUG logs when
|
|
|
|
// level=INFO will get away without any synchronization on most platforms.
|
|
|
|
#ifdef SC_ATOMIC
|
|
|
|
enum sc_log_level curr;
|
|
|
|
|
|
|
|
curr = sc_atomic_load(&sc_log.level);
|
|
|
|
if (level < curr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
sc_log_mutex_lock(&sc_log.mtx);
|
|
|
|
|
2021-02-09 19:12:15 +03:00
|
|
|
#ifndef SC_ATOMIC
|
2020-11-11 01:19:49 +03:00
|
|
|
if (level < sc_log.level) {
|
|
|
|
sc_log_mutex_unlock(&sc_log.mtx);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-09 19:12:15 +03:00
|
|
|
#endif
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
if (sc_log.to_stdout) {
|
|
|
|
va_start(va, fmt);
|
|
|
|
rc |= sc_log_stdout(level, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc_log.fp != NULL) {
|
|
|
|
va_start(va, fmt);
|
|
|
|
rc |= sc_log_file(level, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc_log.cb) {
|
|
|
|
va_start(va, fmt);
|
|
|
|
rc |= sc_log.cb(sc_log.arg, level, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_log_mutex_unlock(&sc_log.mtx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|