mockgallib/src

msg.h

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef MSG_H
#define MSG_H 1

enum LogLevel {msg_debug, msg_verbose, msg_info, msg_warn, msg_error, msg_fatal, msg_silent};

void msg_set_loglevel(const LogLevel level);
void msg_set_prefix(const char prefix[]);

void msg_printf(const LogLevel level, char const * const fmt, ...);
void msg_abort(char const * const fmt, ...);

#endif

msg.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include "msg.h"


static LogLevel log_level= msg_debug;
static char prefix[8]= "";

void msg_set_loglevel(const LogLevel lv)
{
  log_level= lv;
}

void msg_set_prefix(const char prefix_[])
{
  // Start messages with given prefix such as '#'
  strncpy(prefix, prefix_, 7);
}


void msg_printf(const LogLevel msg_level, char const * const fmt, ...)
{
  if(msg_level >= log_level) {
    va_list argp;

    va_start(argp, fmt);

    fprintf(stdout, "%s", prefix);
    vfprintf(stdout, fmt, argp);
    fflush(stdout);

    va_end(argp);
  }
}

void msg_abort(char const * const fmt, ...)
{
  va_list argp;

  if(log_level <= msg_fatal) {
    va_start(argp, fmt);
    vfprintf(stdout, fmt, argp); fflush(stdout);
    va_end(argp);
  }

  abort();
}