~tblue/+junk/biosim

« back to all changes in this revision

Viewing changes to src/log.h

  • Committer: Tilman Blumenbach
  • Date: 2010-02-23 17:20:30 UTC
  • Revision ID: tilman@ax86.net-20100223172030-9s951z97trfik0fs
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file  log.h
 
3
 * @brief Prototypes for logging functions.
 
4
 */
 
5
/* biosim - A simulator for the predator-prey relationship of aphids and
 
6
 *          ladybugs.
 
7
 *
 
8
 * Copyright (c) 2010, Tilman Blumenbach <tilman@ax86.net>
 
9
 * All rights reserved.
 
10
 *
 
11
 * Redistribution and use in source and binary forms, with or without
 
12
 * modification, are permitted provided that the following conditions are
 
13
 * met:
 
14
 *  - Redistributions of source code must retain the above copyright notice,
 
15
 *    this list of conditions and the following disclaimer.
 
16
 *  - Redistributions in binary form must reproduce the above copyright
 
17
 *    notice, this list of conditions and the following disclaimer in the
 
18
 *    documentation and/or other materials provided with the distribution.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
22
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
23
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 
24
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 
28
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
30
 * THE POSSIBILITY OF SUCH DAMAGE.
 
31
 */
 
32
 
 
33
#ifndef LOG_H
 
34
#define LOG_H
 
35
 
 
36
/**
 
37
 * @brief Log a message and the file name / line number of origin to stderr.
 
38
 *
 
39
 * This macro takes a variable number of arguments which are passed to
 
40
 * vfprintf(). That means \c fmt can be a format string.
 
41
 *
 
42
 * This is a wrapper for log_msg().
 
43
 *
 
44
 * @param lvl Log level for this message.
 
45
 * @param fmt The message to log (format string).
 
46
 */
 
47
#define LOGF_FILELINE( lvl, fmt, ... ) log_msg( lvl, "%s:%d: " fmt, \
 
48
        __FILE__, __LINE__, __VA_ARGS__ )
 
49
 
 
50
/**
 
51
 * @brief Log a message and the file name / line number of origin to stderr.
 
52
 *
 
53
 * This macro is similar to LOGF_FILELINE(), but it does not take arguments
 
54
 * to pass to vfprintf() (e. g. for conversion specifiers).
 
55
 *
 
56
 * This is a wrapper for log_msg().
 
57
 * 
 
58
 * @param lvl Log level for this message.
 
59
 * @param fmt The message to log.
 
60
 */
 
61
#define LOG_FILELINE( lvl, fmt ) log_msg( lvl, "%s:%d: " fmt, \
 
62
        __FILE__, __LINE__ )
 
63
 
 
64
/**
 
65
 * @brief The default log level to use if none is set explicitly by calling
 
66
 *        log_set_level().
 
67
 */
 
68
#define LOG_DEFAULT_LVL LOG_ERROR
 
69
 
 
70
typedef enum {
 
71
        LOG_OFF,
 
72
        LOG_ERROR,
 
73
        LOG_INFO,
 
74
        LOG_DEBUG,
 
75
} log_level;
 
76
 
 
77
#ifdef _WIN32
 
78
/**
 
79
 * @brief Print the last Windows API error to stderr.
 
80
 * @param file Path to the file of origin (use __FILE__).
 
81
 * @param line Line number of origin (use __LINE__).
 
82
 */
 
83
void log_last_WinError( const char *file, unsigned int line );
 
84
#endif
 
85
 
 
86
/**
 
87
 * @brief  Get the current log level.
 
88
 * @return The current log level.
 
89
 */
 
90
log_level log_get_level();
 
91
 
 
92
/**
 
93
 * @brief Set the log level.
 
94
 *
 
95
 * The log level determines which log messages are shown to the user.
 
96
 * Messages with a log level higher than the specified one are suppressed.
 
97
 * 
 
98
 * @param The log level to set.
 
99
 */
 
100
void log_set_level( log_level newlvl );
 
101
 
 
102
/**
 
103
 * @brief Log a message to stderr.
 
104
 *
 
105
 * The macros LOGF_FILELINE() and LOG_FILELINE() pass the current file name
 
106
 * and line number to this function and should be used most the time.
 
107
 *
 
108
 * This function takes a variable number of arguments which are passed to
 
109
 * vfprintf(). That means \c fmt can be a format string.
 
110
 * 
 
111
 * @param lvl The log level for the message.
 
112
 * @param fmt The message to log (format string).
 
113
 */
 
114
void log_msg( log_level lvl, const char *fmt, ... );
 
115
 
 
116
#endif /* ifndef LOG_H */