6 n__log_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_log.h

Generic log system. More...

Structures

Name
struct TS_LOG
ThreadSafe LOGging structure.

Detailed Description

Generic log system.

Author: Castagnier Mickael

Date: 2013-04-15

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __LOG_HEADER_GUARD__
#define __LOG_HEADER_GUARD__

#ifdef __cplusplus
extern "C" {
#endif


#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <pthread.h>
/* Before n_common.h, which includes this header: whichever of the two a
 * file includes first, the macro exists before either declares with it. */
#ifndef N_PRINTF_FORMAT
#if defined(__GNUC__) || defined(__clang__)
#if defined(__MINGW32__) && defined(__MINGW_PRINTF_FORMAT)
#define N_PRINTF_FORMAT(fmt, args) __attribute__((format(__MINGW_PRINTF_FORMAT, fmt, args)))
#else
#define N_PRINTF_FORMAT(fmt, args) __attribute__((format(printf, fmt, args)))
#endif
#else
#define N_PRINTF_FORMAT(fmt, args)
#endif
#endif

#include "n_common.h"

#define LOG_NULL -1
#define LOG_FILE -3
#define LOG_STDERR -4
#define LOG_SYSJRNL 100

#if defined(__linux__) || defined(__sun)

#include <syslog.h>
#include <pthread.h>

#ifdef __sun
#include <sys/varargs.h>
#else
#include <stdarg.h>
#endif

#else

#include <stdarg.h>

#define LOG_EMERG 0
#define LOG_ALERT 1
#define LOG_CRIT 2
#define LOG_ERR 3
#define LOG_WARNING 4
#define LOG_NOTICE 5
#define LOG_INFO 6
#define LOG_DEBUG 7

#endif

#define n_log(__LEVEL__, ...)                                         \
    do {                                                              \
        _n_log(__LEVEL__, __FILE__, __func__, __LINE__, __VA_ARGS__); \
    } while (0)

typedef struct TS_LOG {
    pthread_mutex_t LOG_MUTEX;
    FILE* file;
} TS_LOG;

char* open_sysjrnl(const char* identity);
void close_sysjrnl(void);
void set_log_level(const int log_level);
int get_log_level(void);
int set_log_file(char* file);
int set_log_file_fd(FILE* fd);
FILE* get_log_file(void);

void _n_log(int level, const char* file, const char* func, int line, const char* format, ...) N_PRINTF_FORMAT(5, 6);

int open_safe_logging(TS_LOG** log, char* pathname, char* opt);
int write_safe_log(TS_LOG* log, char* pat, ...) N_PRINTF_FORMAT(2, 3);
int close_safe_logging(TS_LOG* log);


#ifdef __cplusplus
}
#endif
#endif