Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_log.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
26
27#ifndef __LOG_HEADER_GUARD__
28#define __LOG_HEADER_GUARD__
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <sys/stat.h>
42#include <pthread.h>
43/* Before n_common.h, which includes this header: whichever of the two a
44 * file includes first, the macro exists before either declares with it. */
45#ifndef N_PRINTF_FORMAT
46#if defined(__GNUC__) || defined(__clang__)
47#if defined(__MINGW32__) && defined(__MINGW_PRINTF_FORMAT)
53#define N_PRINTF_FORMAT(fmt, args) __attribute__((format(__MINGW_PRINTF_FORMAT, fmt, args)))
54#else
59#define N_PRINTF_FORMAT(fmt, args) __attribute__((format(printf, fmt, args)))
60#endif
61#else
63#define N_PRINTF_FORMAT(fmt, args)
64#endif
65#endif
66
67#include "n_common.h"
68
70#define LOG_NULL -1
72#define LOG_FILE -3
74#define LOG_STDERR -4
76#define LOG_SYSJRNL 100
77
78#if defined(__linux__) || defined(__sun)
79
80#include <syslog.h>
81#include <pthread.h>
82
83#ifdef __sun
84#include <sys/varargs.h>
85#else
86#include <stdarg.h>
87#endif
88
89#else
90
91#include <stdarg.h>
92
94#define LOG_EMERG 0
96#define LOG_ALERT 1
98#define LOG_CRIT 2
100#define LOG_ERR 3
102#define LOG_WARNING 4
104#define LOG_NOTICE 5
106#define LOG_INFO 6
108#define LOG_DEBUG 7
109
110#endif
111
113#define n_log(__LEVEL__, ...) \
114 do { \
115 _n_log(__LEVEL__, __FILE__, __func__, __LINE__, __VA_ARGS__); \
116 } while (0)
117
119typedef struct TS_LOG {
121 pthread_mutex_t LOG_MUTEX;
123 FILE* file;
124} TS_LOG;
125
127char* open_sysjrnl(const char* identity);
129void close_sysjrnl(void);
131void set_log_level(const int log_level);
133int get_log_level(void);
135int set_log_file(char* file);
137int set_log_file_fd(FILE* fd);
139FILE* get_log_file(void);
140
142void _n_log(int level, const char* file, const char* func, int line, const char* format, ...) N_PRINTF_FORMAT(5, 6);
143
145int open_safe_logging(TS_LOG** log, char* pathname, char* opt);
147int write_safe_log(TS_LOG* log, char* pat, ...) N_PRINTF_FORMAT(2, 3);
149int close_safe_logging(TS_LOG* log);
150
154
155#ifdef __cplusplus
156}
157#endif
158#endif
static int log_level
void line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color)
draw a line
pthread_mutex_t LOG_MUTEX
mutex for thread-safe writting
Definition n_log.h:121
FILE * file
File handler.
Definition n_log.h:123
FILE * get_log_file(void)
get the log file name if any
Definition n_log.c:227
int write_safe_log(TS_LOG *log, char *pat,...)
write to a thread-safe logging file
Definition n_log.c:482
int open_safe_logging(TS_LOG **log, char *pathname, char *opt)
Open a thread-safe logging file.
Definition n_log.c:413
void close_sysjrnl(void)
close syslog / free name for event log
Definition n_log.c:112
char * open_sysjrnl(const char *identity)
Open a syslog / set name for event log.
Definition n_log.c:96
int set_log_file_fd(FILE *fd)
set an already open stream (stdout, stderr, a caller's FILE) as standard log output,...
Definition n_log.c:210
#define N_PRINTF_FORMAT(fmt, args)
declare a function printf-like; nothing on a compiler without attributes
Definition n_log.h:63
int close_safe_logging(TS_LOG *log)
close a thread-safe logging file
Definition n_log.c:510
int set_log_file(char *file)
set a file as standard log output
Definition n_log.c:170
void set_log_level(const int log_level)
Set global log level.
Definition n_log.c:123
void _n_log(int level, const char *file, const char *func, int line, const char *format,...)
Full log function.
Definition n_log.c:298
int get_log_level(void)
Return the global log level.
Definition n_log.c:161
ThreadSafe LOGging structure.
Definition n_log.h:119
Common headers and low-level functions & define.