Program Listing for File cmb_logger.h

Return to documentation for file (include/cmb_logger.h)

/*
 * Copyright (c) Asbjørn M. Bonvik 1993-1995, 2025-26.
 *
 * 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.
 */

#ifndef CIMBA_CMB_LOGGER_H
#define CIMBA_CMB_LOGGER_H

#include <stdint.h>
#include <stdio.h>

#include "cmi_config.h"

#define CMB_LOGGER_FATAL    UINT32_C(0x80000000)
#define CMB_LOGGER_ERROR    UINT32_C(0x40000000)
#define CMB_LOGGER_WARNING  UINT32_C(0x20000000)
#define CMB_LOGGER_INFO     UINT32_C(0x10000000)

 extern void cmb_logger_flags_on(uint32_t flags);

extern void cmb_logger_flags_off(uint32_t flags);

typedef const char *(cmb_timeformatter_func)(double t);

extern void cmb_logger_timeformatter_set(cmb_timeformatter_func *tf);

extern int cmb_logger_vfprintf(FILE *fp,
                               uint32_t flags,
                               const char *func,
                               int line,
                               const char *fmtstr,
                               va_list args)
                                    __attribute__((format(printf, 3, 0)));

/*
 * Wrapper functions for predefined message levels.
 * cmb_logger_fatal() terminates the entire simulation,
 * cmb_logger_error() terminates the current replication thread only.
 *
 * Use appropriate function attributes to avoid spurious compiler warnings in
 * unreachable code. No portable way to do this more elegantly, unfortunately.
 */

#define cmb_logger_fatal(fp, fmtstr, ...) \
    cmi_logger_fatal(fp, __func__, __LINE__, fmtstr,##__VA_ARGS__)

#define cmb_logger_error(fp, fmtstr, ...) \
    cmi_logger_error(fp, __func__, __LINE__, fmtstr, ##__VA_ARGS__)

#define cmb_logger_warning(fp, fmtstr, ...) \
    cmi_logger_warning(fp, __func__, __LINE__, fmtstr, ##__VA_ARGS__)

#ifndef NLOGINFO
  #define cmb_logger_info(fp, fmtstr, ...) \
    cmi_logger_info(fp, __func__, __LINE__, fmtstr, ##__VA_ARGS__)
#else
  #define cmb_logger_info(fp, fmtstr, ...) ((void)(0))
#endif

#define cmb_logger_user(fp, flags, fmtstr, ...) \
    cmi_logger_user(fp, flags, __func__, __LINE__, fmtstr, ##__VA_ARGS__)

/* The trial index is maintained by the worker threads in cimba.c, we use it in logging messages */
extern CMB_THREAD_LOCAL uint64_t cmi_logger_trial_idx;

/* Actual functions wrapped by the macros above */
extern void cmi_logger_fatal(FILE *fp, const char *func, int line, char *fmtstr, ...)
                      __attribute__((noreturn, format(printf,4,5)));
extern void cmi_logger_error(FILE *fp, const char *func, int line, char *fmtstr, ...)
                      __attribute__((noreturn, format(printf,4,5)));
extern void cmi_logger_warning(FILE *fp, const char *func, int line, char *fmtstr, ...)
                      __attribute__((format(printf,4,5)));
extern void cmi_logger_info(FILE *fp, const char *func, int line, char *fmtstr, ...)
                      __attribute__((format(printf,4,5)));
extern void cmi_logger_user(FILE *fp, uint32_t flags, const char *func, int line, char *fmtstr, ...)
                      __attribute__((format(printf,5,6)));
#endif /* CIMBA_CMB_LOGGER_H */