home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / asm / log.h < prev    next >
C/C++ Source or Header  |  2008-04-01  |  4KB  |  134 lines

  1. #ifndef ALREADY_INCLUDED_LOG
  2. #define ALREADY_INCLUDED_LOG
  3. /*
  4.  * Copyright (c) 2002, 2003 Magnus Lind.
  5.  *
  6.  * This software is provided 'as-is', without any express or implied warranty.
  7.  * In no event will the authors be held liable for any damages arising from
  8.  * the use of this software.
  9.  *
  10.  * Permission is granted to anyone to use this software, alter it and re-
  11.  * distribute it freely for any non-commercial, non-profit purpose subject to
  12.  * the following restrictions:
  13.  *
  14.  *   1. The origin of this software must not be misrepresented; you must not
  15.  *   claim that you wrote the original software. If you use this software in a
  16.  *   product, an acknowledgment in the product documentation would be
  17.  *   appreciated but is not required.
  18.  *
  19.  *   2. Altered source versions must be plainly marked as such, and must not
  20.  *   be misrepresented as being the original software.
  21.  *
  22.  *   3. This notice may not be removed or altered from any distribution.
  23.  *
  24.  *   4. The names of this software and/or it's copyright holders may not be
  25.  *   used to endorse or promote products derived from this software without
  26.  *   specific prior written permission.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdarg.h>
  32.  
  33. enum log_level {
  34.     LOG_MIN = -99,
  35.     LOG_FATAL = -40,
  36.     LOG_ERROR = -30,
  37.     LOG_WARNING = -20,
  38.     LOG_BRIEF = -10,
  39.     LOG_NORMAL = 0,
  40.     LOG_VERBOSE = 10,
  41.     LOG_TRACE = 20,
  42.     LOG_DEBUG = 30,
  43.     LOG_DUMP = 40,
  44.     LOG_MAX = 99
  45. };
  46.  
  47. typedef
  48. void log_formatter_f(FILE * out,        /* IN */
  49.                      enum log_level level,      /* IN */
  50.                      const char *context,       /* IN */
  51.                      const char *);     /* IN */
  52.  
  53. /*
  54.  * this log output function adds nothing
  55.  */
  56. void raw_log_formatter(FILE * out,      /* IN */
  57.                        enum log_level level,    /* IN */
  58.                        const char *context,     /* IN */
  59.                        const char *log);        /* IN */
  60.  
  61.  
  62. struct log_output;
  63.  
  64. struct log_ctx;
  65.  
  66. struct log_ctx *log_new(void);
  67.  
  68. /* log_delete closes all added output streams
  69.  * and files except for stdout and stderr
  70.  */
  71. void log_delete(struct log_ctx *ctx);
  72.  
  73. void log_set_level(struct log_ctx *ctx, /* IN/OUT */
  74.                    enum log_level level);       /* IN */
  75.  
  76. void log_add_output_stream(struct log_ctx *ctx, /* IN/OUT */
  77.                            enum log_level min,  /* IN */
  78.                            enum log_level max,  /* IN */
  79.                            log_formatter_f * default_f, /* IN */
  80.                            FILE * out_stream);  /* IN */
  81.  
  82. void log_vlog(struct log_ctx *ctx,      /* IN */
  83.               enum log_level level,     /* IN */
  84.               const char *context,      /* IN */
  85.               log_formatter_f * f,      /* IN */
  86.               const char *printf_str,   /* IN */
  87.               va_list argp);
  88.  
  89.  
  90. void log_log_default(const char *printf_str,    /* IN */
  91.                      ...);
  92.  
  93. /* some helper macros */
  94.  
  95. extern struct log_ctx *G_log_ctx;
  96. extern enum log_level G_log_level;
  97. extern enum log_level G_log_log_level;
  98.  
  99. #define LOG_SET_LEVEL(L) \
  100. do { \
  101.     log_set_level(G_log_ctx, (L)); \
  102.     G_log_level = (L); \
  103. } while(0)
  104.  
  105. #define LOG_INIT(L) \
  106. do { \
  107.     G_log_ctx = log_new(); \
  108.     log_set_level(G_log_ctx, (L)); \
  109.     G_log_level = (L); \
  110. } while(0)
  111.  
  112. #define LOG_INIT_CONSOLE(X) \
  113. do { \
  114.     G_log_ctx = log_new(); \
  115.     log_set_level(G_log_ctx, (X)); \
  116.     G_log_level = (X); \
  117.     log_add_output_stream(G_log_ctx, LOG_WARNING, LOG_MAX, NULL, stdout); \
  118.     log_add_output_stream(G_log_ctx, LOG_MIN, LOG_WARNING - 1, NULL, stderr); \
  119. } while(0)
  120.  
  121. #define LOG_FREE log_delete(G_log_ctx)
  122.  
  123. #define IS_LOGGABLE(L) (G_log_level >= (L))
  124.  
  125. #define LOG(L, M) \
  126. do { \
  127.     if(IS_LOGGABLE(L)) { \
  128.         G_log_log_level = (L); \
  129.         log_log_default M; \
  130.     } \
  131. } while(0)
  132.  
  133. #endif
  134.