|
yast2-core
|
00001 /* y2log.h 00002 * 00003 * YaST2: Core system 00004 * 00005 * YaST2 logging implementation 00006 * 00007 * Authors: Mathias Kettner <kettner@suse.de> 00008 * Michal Svec <msvec@suse.cz> 00009 * 00010 * $Id: y2log.h 46620 2008-04-14 18:48:50Z mvidner $ 00011 */ 00012 00013 #ifndef _y2log_h 00014 #define _y2log_h 00015 00016 #include <string> 00017 #include <stdio.h> 00018 00019 using std::string; 00020 00021 /* Logging levels */ 00022 00023 enum loglevel_t { 00024 LOG_DEBUG = 0, // debug message 00025 LOG_MILESTONE = 1, // log great events, big steps 00026 LOG_WARNING = 2, // warning in application level 00027 LOG_ERROR = 3, // error in application level 00028 LOG_SECURITY = 4, // security relevant problem or incident 00029 LOG_INTERNAL = 5 // internal bug. Please report to... 00030 }; 00031 00032 /* Logging functions */ 00033 00034 // Implements y2_logger 00035 void y2_logger_function (loglevel_t level, const char *component, const char *file, 00036 const int line, const char *func, const char *format, ...) 00037 __attribute__ ((format (printf, 6, 7))); 00038 // The knights of Blanik only show up when nothing else can help, and so will 00039 // the messages logged here. fate#302166 00040 void y2_logger_blanik (loglevel_t level, const char *component, const char *file, 00041 const int line, const char *func, const char *format, ...) 00042 __attribute__ ((format (printf, 6, 7))); 00043 00044 // Same as above, but with va_list 00045 void y2_vlogger_function (loglevel_t level, const char *component, const char *file, 00046 const int line, const char *func, const char *format, va_list ap); 00047 void y2_vlogger_blanik (loglevel_t level, const char *component, const char *file, 00048 const int line, const char *func, const char *format, va_list ap); 00049 00050 void y2_logger_raw( const char* message ); 00051 00052 /* Logging defines */ 00053 00054 #ifdef y2log_subcomponent 00055 # define y2log_suffix "-" y2log_subcomponent 00056 #else 00057 # define y2log_suffix 00058 #endif 00059 00060 #ifdef y2log_component 00061 # define y2log_prefix y2log_component y2log_suffix 00062 #else 00063 # ifdef Y2LOG 00064 # define y2log_prefix Y2LOG y2log_suffix 00065 # else 00066 # error neither y2log_component nor Y2LOG defined 00067 # define y2log_prefix "" 00068 # endif 00069 #endif 00070 00071 #define y2_logger(level,comp,file,line,function,format,args...) \ 00072 do { \ 00073 if (should_be_logged (level, comp)) \ 00074 y2_logger_function (level,comp,file,line,function,format,##args);\ 00075 else if (should_be_buffered ()) \ 00076 y2_logger_blanik (level,comp,file,line,function,format,##args); \ 00077 } while (0) 00078 00079 #define y2_vlogger(level,comp,file,line,function,format,args) \ 00080 do { \ 00081 if (should_be_logged (level, comp)) \ 00082 y2_vlogger_function (level,comp,file,line,function,format,args);\ 00083 else if (should_be_buffered ()) \ 00084 y2_vlogger_blanik (level,comp,file,line,function,format,args); \ 00085 } while (0) 00086 00087 /* 00088 * Caution: Don't use 00089 * if (shouldbelogged(...) y2_logger(...) 00090 * above - this clashes with any 00091 * if (...) 00092 * y2error(...) 00093 * else 00094 * since the "else" branch always refers to the inner (!) "if" 00095 * - in this case, the "if" of this macro :-(( 00096 */ 00097 00098 #define y2logger(level, format, args...) \ 00099 y2_logger(level,y2log_prefix,__FILE__,__LINE__,__FUNCTION__,format,##args) 00100 00101 #define y2vlogger(level, format, ap) \ 00102 y2_vlogger(level,y2log_prefix,__FILE__,__LINE__,__FUNCTION__,format,ap) 00103 00104 #ifdef WITHOUT_Y2DEBUG 00105 # define y2debug(format, args...) 00106 #else 00107 # define y2debug(format, args...) y2logger(LOG_DEBUG,format,##args) 00108 #endif 00109 00110 #define y2milestone(format, args...) y2logger(LOG_MILESTONE,format,##args) 00111 #define y2warning(format, args...) y2logger(LOG_WARNING,format,##args) 00112 #define y2error(format, args...) y2logger(LOG_ERROR,format,##args) 00113 #define y2security(format, args...) y2logger(LOG_SECURITY,format,##args) 00114 #define y2internal(format, args...) y2logger(LOG_INTERNAL,format,##args) 00115 00116 #define y2lograw(message) y2_logger_raw(message) 00117 00121 bool should_be_logged (int loglevel, string componentname); 00125 bool should_be_buffered (); 00126 00135 void set_log_filename (string filename); 00136 string get_log_filename(); 00137 00144 void set_log_conf(string confname); 00145 00149 void set_log_simple_mode(bool simple); 00150 00155 void set_log_debug(bool on = true); 00156 00160 bool get_log_debug(); 00161 00162 // stores a few strings. can append one. can return all. old are forgotten. 00163 class LogTail { 00164 public: 00165 typedef string Data; 00166 LogTail (size_t max_size = 42); 00167 ~LogTail (); 00168 void push_back (const Data &); 00169 00170 // consumer returns true to continue iterating 00171 typedef bool (* Consumer) (const Data &); 00172 void for_each (Consumer c); 00173 private: 00174 class Impl; 00175 Impl *m_impl; 00176 }; 00177 00178 // the instance used for last resort logging 00179 extern LogTail blanik; 00180 00181 #endif /* _y2log_h */
1.7.3