sc/logger/sc_log.h

126 lines
4.0 KiB
C
Raw Normal View History

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.
*/
#ifndef SC_LOG_H
#define SC_LOG_H
2021-02-06 21:36:18 +03:00
#include <stdarg.h>
2020-11-11 01:19:49 +03:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-04-13 22:36:50 +03:00
#define SC_LOG_VERSION "2.0.0"
2021-02-14 16:19:26 +03:00
2020-11-11 01:19:49 +03:00
enum sc_log_level
{
SC_LOG_DEBUG,
SC_LOG_INFO,
SC_LOG_WARN,
SC_LOG_ERROR,
SC_LOG_OFF,
2020-11-11 01:19:49 +03:00
};
// clang-format off
2021-02-07 22:31:04 +03:00
static const struct sc_log_level_pair
2020-11-11 01:19:49 +03:00
{
2021-01-24 17:56:18 +03:00
const enum sc_log_level id;
2020-11-11 01:19:49 +03:00
const char *str;
} sc_log_levels[] = {
{SC_LOG_DEBUG, "DEBUG"},
{SC_LOG_INFO, "INFO" },
{SC_LOG_WARN, "WARN" },
{SC_LOG_ERROR, "ERROR"},
{SC_LOG_OFF, "OFF" },
};
// clang-format on
2021-02-06 21:36:18 +03:00
// Internal function
int sc_log_log(enum sc_log_level level, const char *fmt, ...);
2020-11-11 01:19:49 +03:00
2021-02-06 21:36:18 +03:00
// Max file size to rotate, should not be more than 4 GB.
#define SC_LOG_FILE_SIZE (2 * 1024 * 1024)
2020-11-11 01:19:49 +03:00
2021-02-06 21:36:18 +03:00
// Define SC_LOG_PRINT_FILE_NAME to print file name and line no in the log line.
#ifdef SC_LOG_PRINT_FILE_NAME
#define sc_log_ap(fmt, ...) \
"(%s:%d) " fmt, strrchr("/" __FILE__, '/') + 1, __LINE__, __VA_ARGS__
2021-02-06 21:36:18 +03:00
#else
#define sc_log_ap(fmt, ...) fmt, __VA_ARGS__
2021-02-06 21:36:18 +03:00
#endif
2020-11-11 01:19:49 +03:00
/**
2021-02-06 21:36:18 +03:00
* sc_log_init() call once in your application before using log functions.
* sc_log_term() call once to clean up, you must not use logger after this call.
* These functions are not thread-safe, should be called from a single thread.
*
* @return '0' on success, negative value on error, errno will be set.
*/
2021-02-06 21:36:18 +03:00
int sc_log_init(void);
int sc_log_term(void);
/**
2021-02-06 21:36:18 +03:00
* Call once from each thread if you want to set thread name.
* @param name Thread name
*/
2021-02-06 21:36:18 +03:00
void sc_log_set_thread_name(const char *name);
2020-11-11 01:19:49 +03:00
/**
* @param level One of "DEBUG", "INFO", "WARN", "ERROR", "OFF"
* @return '0' on success, negative value on invalid level string
2020-11-11 01:19:49 +03:00
*/
int sc_log_set_level(const char *level);
2020-11-11 01:19:49 +03:00
/**
2021-02-06 21:36:18 +03:00
* @param enable 'true' to enable, 'false' to disable logging to stdout.
2020-11-11 01:19:49 +03:00
*/
2021-02-06 21:36:18 +03:00
void sc_log_set_stdout(bool enable);
2020-11-11 01:19:49 +03:00
/**
2021-02-06 21:36:18 +03:00
* Log files will be rotated. Latest logs will always be in the 'current_file'.
2020-11-11 01:19:49 +03:00
* e.g sc_log_set_file("/tmp/log.0.txt", "/tmp/log-latest.txt");
2021-02-06 21:36:18 +03:00
* To disable logging into file : sc_log_set_file(NULL, NULL);
2020-11-11 01:19:49 +03:00
*
* @param prev file path for previous log file, 'NULL' to disable
* @param current file path for latest log file, 'NULL' to disable
* @return 0 on success, -1 on error, errno will be set.
2020-11-11 01:19:49 +03:00
*/
int sc_log_set_file(const char *prev, const char *current);
2020-11-11 01:19:49 +03:00
/**
2021-02-06 21:36:18 +03:00
* @param arg user arg to callback.
* @param cb log callback.
2020-11-11 01:19:49 +03:00
*/
2021-02-06 21:36:18 +03:00
void sc_log_set_callback(void *arg,
int (*cb)(void *arg, enum sc_log_level level,
const char *fmt, va_list va));
2020-11-11 01:19:49 +03:00
2021-02-06 21:36:18 +03:00
// e.g : sc_log_error("Errno : %d, reason : %s", errno, strerror(errno));
2020-11-11 01:19:49 +03:00
#define sc_log_debug(...) (sc_log_log(SC_LOG_DEBUG, sc_log_ap(__VA_ARGS__, "")))
#define sc_log_info(...) (sc_log_log(SC_LOG_INFO, sc_log_ap(__VA_ARGS__, "")))
#define sc_log_warn(...) (sc_log_log(SC_LOG_WARN, sc_log_ap(__VA_ARGS__, "")))
2020-11-11 01:19:49 +03:00
#define sc_log_error(...) (sc_log_log(SC_LOG_ERROR, sc_log_ap(__VA_ARGS__, "")))
#endif