1 /*
   2  * logging.h - Centralised logging. Part of the Linux-NTFS project.
   3  *
   4  * Copyright (c) 2005 Richard Russon
   5  *
   6  * This program/include file is free software; you can redistribute it and/or
   7  * modify it under the terms of the GNU General Public License as published
   8  * by the Free Software Foundation; either version 2 of the License, or
   9  * (at your option) any later version.
  10  *
  11  * This program/include file is distributed in the hope that it will be
  12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  *
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program (in the main directory of the Linux-NTFS
  18  * distribution in the file COPYING); if not, write to the Free Software
  19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  */
  21 
  22 #ifndef _LOGGING_H_
  23 #define _LOGGING_H_
  24 
  25 #ifdef HAVE_CONFIG_H
  26 #include "config.h"
  27 #endif
  28 
  29 #ifdef HAVE_STDARG_H
  30 #include <stdarg.h>
  31 #endif
  32 
  33 #include "types.h"
  34 
  35 /* Function prototype for the logging handlers */
  36 typedef int (ntfs_log_handler)(const char *function, const char *file, int line,
  37         u32 level, void *data, const char *format, va_list args);
  38 
  39 /* Set the logging handler from one of the functions, below. */
  40 void ntfs_log_set_handler(ntfs_log_handler *handler);
  41 
  42 /* Logging handlers */
  43 ntfs_log_handler ntfs_log_handler_syslog  __attribute__((format(printf, 6, 0)));
  44 ntfs_log_handler ntfs_log_handler_fprintf __attribute__((format(printf, 6, 0)));
  45 ntfs_log_handler ntfs_log_handler_null    __attribute__((format(printf, 6, 0)));
  46 ntfs_log_handler ntfs_log_handler_stdout  __attribute__((format(printf, 6, 0)));
  47 ntfs_log_handler ntfs_log_handler_outerr  __attribute__((format(printf, 6, 0)));
  48 ntfs_log_handler ntfs_log_handler_stderr  __attribute__((format(printf, 6, 0)));
  49 
  50 /* Enable/disable certain log levels */
  51 u32 ntfs_log_set_levels(u32 levels);
  52 u32 ntfs_log_clear_levels(u32 levels);
  53 u32 ntfs_log_get_levels(void);
  54 
  55 /* Enable/disable certain log flags */
  56 u32 ntfs_log_set_flags(u32 flags);
  57 u32 ntfs_log_clear_flags(u32 flags);
  58 u32 ntfs_log_get_flags(void);
  59 
  60 /* Turn command-line options into logging flags */
  61 BOOL ntfs_log_parse_option(const char *option);
  62 
  63 int ntfs_log_redirect(const char *function, const char *file, int line,
  64         u32 level, void *data, const char *format, ...)
  65         __attribute__((format(printf, 6, 7)));
  66 
  67 /* Logging levels - Determine what gets logged */
  68 #define NTFS_LOG_LEVEL_DEBUG    ((u32)1 <<  0) /* x = 42 */
  69 #define NTFS_LOG_LEVEL_TRACE    ((u32)1 <<  1) /* Entering function x() */
  70 #define NTFS_LOG_LEVEL_QUIET    ((u32)1 <<  2) /* Quietable output */
  71 #define NTFS_LOG_LEVEL_INFO     ((u32)1 <<  3) /* Volume needs defragmenting */
  72 #define NTFS_LOG_LEVEL_VERBOSE  ((u32)1 <<  4) /* Forced to continue */
  73 #define NTFS_LOG_LEVEL_PROGRESS ((u32)1 <<  5) /* 54% complete */
  74 #define NTFS_LOG_LEVEL_WARNING  ((u32)1 <<  6) /* You should backup before starting */
  75 #define NTFS_LOG_LEVEL_ERROR    ((u32)1 <<  7) /* Operation failed, no damage done */
  76 #define NTFS_LOG_LEVEL_PERROR   ((u32)1 <<  8) /* Message : standard error description */
  77 #define NTFS_LOG_LEVEL_CRITICAL ((u32)1 <<  9) /* Operation failed,damage may have occurred */
  78 
  79 /* Logging style flags - Manage the style of the output */
  80 #define NTFS_LOG_FLAG_PREFIX    ((u32)1 << 0) /* Prefix messages with "ERROR: ", etc */
  81 #define NTFS_LOG_FLAG_FILENAME  ((u32)1 << 1) /* Show the file origin of the message */
  82 #define NTFS_LOG_FLAG_LINE      ((u32)1 << 2) /* Show the line number of the message */
  83 #define NTFS_LOG_FLAG_FUNCTION  ((u32)1 << 3) /* Show the function name containing the message */
  84 #define NTFS_LOG_FLAG_ONLYNAME  ((u32)1 << 4) /* Only display the filename, not the pathname */
  85 #define NTFS_LOG_FLAG_COLOUR    ((u32)1 << 5) /* Colour highlight some messages */
  86 
  87 /* Macros to simplify logging.  One for each level defined above.
  88  * Note, if DEBUG is not defined, then ntfs_log_debug/trace have no effect.
  89  */
  90 #if defined(__GNUC__)
  91 
  92 #define ntfs_log_critical(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_CRITICAL,NULL,FORMAT,##ARGS)
  93 #define ntfs_log_error(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ERROR,NULL,FORMAT,##ARGS)
  94 #define ntfs_log_info(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_INFO,NULL,FORMAT,##ARGS)
  95 #define ntfs_log_perror(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PERROR,NULL,FORMAT,##ARGS)
  96 #define ntfs_log_progress(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PROGRESS,NULL,FORMAT,##ARGS)
  97 #define ntfs_log_quiet(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_QUIET,NULL,FORMAT,##ARGS)
  98 #define ntfs_log_verbose(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_VERBOSE,NULL,FORMAT,##ARGS)
  99 #define ntfs_log_warning(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_WARNING,NULL,FORMAT,##ARGS)
 100 
 101 #else /* not __GNUC__ */
 102 
 103 #define PRINT(...) printf(__VA_ARGS__)
 104 
 105 #define ntfs_log_critical(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_CRITICAL,NULL,__VA_ARGS__)
 106 #define ntfs_log_error(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_ERROR,NULL,__VA_ARGS__)
 107 #define ntfs_log_info(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_INFO,NULL,__VA_ARGS__)
 108 #define ntfs_log_perror(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_PERROR,NULL,__VA_ARGS__)
 109 #define ntfs_log_progress(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_PROGRESS,NULL,__VA_ARGS__)
 110 #define ntfs_log_quiet(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_QUIET,NULL,__VA_ARGS__)
 111 #define ntfs_log_verbose(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_VERBOSE,NULL,__VA_ARGS__)
 112 #define ntfs_log_warning(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_WARNING,NULL,__VA_ARGS__)
 113 
 114 #endif /* __GNUC__ */
 115 
 116 /*
 117  * By default debug and trace messages are compiled into the program,
 118  * but not displayed.
 119  */
 120 #if defined(__GNUC__)
 121 
 122 #ifndef DEBUG
 123 #define ntfs_log_debug(FORMAT, ARGS...)do {} while (0)
 124 #define ntfs_log_trace(FORMAT, ARGS...)do {} while (0)
 125 #else /* !DEBUG */
 126 #define ntfs_log_debug(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_DEBUG,NULL,FORMAT,##ARGS)
 127 #define ntfs_log_trace(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_TRACE,NULL,FORMAT,##ARGS)
 128 #endif /* DEBUG */
 129 
 130 #else /* not __GNUC__ */
 131 
 132 #ifndef DEBUG
 133 #define ntfs_log_debug(...) do {} while (0)
 134 #define ntfs_log_trace(...) do {} while (0)
 135 #else /* !DEBUG */
 136 #define ntfs_log_debug(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_DEBUG,NULL,__VA_ARGS__)
 137 #define ntfs_log_trace(...) ntfs_log_redirect("unknown",__FILE__,__LINE__,NTFS_LOG_LEVEL_TRACE,NULL,__VA_ARGS__)
 138 #endif /* DEBUG */
 139 
 140 #endif /* __GNUC__ */
 141 
 142 #endif /* _LOGGING_H_ */
 143