00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __CIO_H__
00013 #define __CIO_H__
00014
00015 #include <stdio.h>
00016 #include <stdarg.h>
00017 #include <string.h>
00018 #include <dirent.h>
00019 #include <unistd.h>
00020
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023
00024 #include "lib/common.h"
00025 #include "base/init.h"
00026
00027 namespace shogun
00028 {
00029 class CIO;
00030 extern CIO* sg_io;
00031 }
00032
00033
00034 namespace shogun
00035 {
00040 enum EMessageType
00041 {
00042 MSG_GCDEBUG,
00043 MSG_DEBUG,
00044 MSG_INFO,
00045 MSG_NOTICE,
00046 MSG_WARN,
00047 MSG_ERROR,
00048 MSG_CRITICAL,
00049 MSG_ALERT,
00050 MSG_EMERGENCY,
00051 MSG_MESSAGEONLY
00052 };
00053
00054
00055 #define NUM_LOG_LEVELS 10
00056 #define FBUFSIZE 4096
00057
00058 #ifdef DARWIN
00059 #define CONST_DIRENT_T struct dirent
00060 #else //DARWIN
00061 #define CONST_DIRENT_T const struct dirent
00062 #endif //DARWIN
00063
00064
00065
00066 #define SG_GCDEBUG(...) io->message(MSG_GCDEBUG, __FILE__, __LINE__, __VA_ARGS__)
00067 #define SG_DEBUG(...) io->message(MSG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
00068 #define SG_INFO(...) io->message(MSG_INFO, __FILE__, __LINE__, __VA_ARGS__)
00069 #define SG_WARNING(...) io->message(MSG_WARN, __FILE__, __LINE__, __VA_ARGS__)
00070 #define SG_ERROR(...) io->message(MSG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
00071
00072 #define SG_PRINT(...) io->message(MSG_MESSAGEONLY, __FILE__, __LINE__, __VA_ARGS__)
00073 #define SG_NOTIMPLEMENTED io->not_implemented(__FILE__, __LINE__)
00074 #define SG_DEPRECATED io->deprecated(__FILE__, __LINE__)
00075
00076 #define SG_PROGRESS(...) io->progress(__VA_ARGS__)
00077 #define SG_ABS_PROGRESS(...) io->absolute_progress(__VA_ARGS__)
00078 #define SG_DONE() io->done()
00079
00080
00081 #define SG_SGCDEBUG(...) sg_io->message(MSG_GCDEBUG,__FILE__, __LINE__, __VA_ARGS__)
00082 #define SG_SDEBUG(...) sg_io->message(MSG_DEBUG,__FILE__, __LINE__, __VA_ARGS__)
00083 #define SG_SINFO(...) sg_io->message(MSG_INFO,__FILE__, __LINE__, __VA_ARGS__)
00084 #define SG_SWARNING(...) sg_io->message(MSG_WARN,__FILE__, __LINE__, __VA_ARGS__)
00085 #define SG_SERROR(...) sg_io->message(MSG_ERROR,__FILE__, __LINE__, __VA_ARGS__)
00086 #define SG_SPRINT(...) sg_io->message(MSG_MESSAGEONLY,__FILE__, __LINE__, __VA_ARGS__)
00087 #define SG_SPROGRESS(...) sg_io->progress(__VA_ARGS__)
00088 #define SG_SABS_PROGRESS(...) sg_io->absolute_progress(__VA_ARGS__)
00089 #define SG_SDONE() sg_io->done()
00090 #define SG_SNOTIMPLEMENTED sg_io->not_implemented(__FILE__, __LINE__)
00091 #define SG_SDEPRECATED sg_io->deprecated(__FILE__, __LINE__)
00092
00093 #define ASSERT(x) { if (!(x)) SG_SERROR("assertion %s failed in file %s line %d\n",#x, __FILE__, __LINE__);}
00094
00095
00102 class CIO
00103 {
00104 public:
00106 CIO();
00108 CIO(const CIO& orig);
00109
00114 void set_loglevel(EMessageType level);
00115
00120 EMessageType get_loglevel() const;
00121
00126 inline bool get_show_progress() const
00127 {
00128 return show_progress;
00129 }
00130
00135 inline bool get_show_file_and_line() const
00136 {
00137 return show_file_and_line;
00138 }
00139
00150 void message(EMessageType prio, const char* file,
00151 int32_t line, const char *fmt, ... ) const;
00152
00161 void progress(
00162 float64_t current_val,
00163 float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
00164 const char* prefix="PROGRESS:\t");
00165
00175 void absolute_progress(
00176 float64_t current_val, float64_t val,
00177 float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
00178 const char* prefix="PROGRESS:\t");
00179
00184 void done();
00185
00187 inline void not_implemented(const char* file, int32_t line) const
00188 {
00189 message(MSG_ERROR, file, line, "Sorry, not yet implemented .\n");
00190 }
00191
00193 inline void deprecated(const char* file, int32_t line) const
00194 {
00195 message(MSG_WARN, file, line,
00196 "This function is deprecated and will be removed soon.\n");
00197 }
00198
00204 void buffered_message(EMessageType prio, const char *fmt, ... ) const;
00205
00211 static char* skip_spaces(char* str);
00212
00218 static char* skip_blanks(char* str);
00219
00224 inline FILE* get_target() const
00225 {
00226 return target;
00227 }
00228
00233 void set_target(FILE* target);
00234
00236 inline void set_target_to_stderr() { set_target(stderr); }
00237
00239 inline void set_target_to_stdout() { set_target(stdout); }
00240
00242 inline void enable_progress()
00243 {
00244 show_progress=true;
00245
00246
00247 if (sg_io!=this)
00248 sg_io->enable_progress();
00249 }
00250
00252 inline void disable_progress()
00253 {
00254 show_progress=false;
00255
00256
00257 if (sg_io!=this)
00258 sg_io->disable_progress();
00259 }
00260
00262 inline void enable_file_and_line()
00263 {
00264 show_file_and_line=true;
00265
00266 if (sg_io!=this)
00267 sg_io->enable_file_and_line();
00268 }
00269
00271 inline void disable_file_and_line()
00272 {
00273 show_file_and_line=false;
00274
00275 if (sg_io!=this)
00276 sg_io->disable_file_and_line();
00277 }
00278
00283 static inline void set_dirname(const char* dirname)
00284 {
00285 strncpy(directory_name, dirname, FBUFSIZE);
00286 }
00287
00294 static inline char* concat_filename(const char* filename)
00295 {
00296 if (snprintf(file_buffer, FBUFSIZE, "%s/%s", directory_name, filename) > FBUFSIZE)
00297 SG_SERROR("filename too long");
00298 SG_SDEBUG("filename=\"%s\"\n", file_buffer);
00299 return file_buffer;
00300 }
00301
00307 static inline int filter(CONST_DIRENT_T* d)
00308 {
00309 if (d)
00310 {
00311 char* fname=concat_filename(d->d_name);
00312
00313 if (!access(fname, R_OK))
00314 {
00315 struct stat s;
00316 if (!stat(fname, &s) && S_ISREG(s.st_mode))
00317 return 1;
00318 }
00319 }
00320
00321 return 0;
00322 }
00323
00328 inline int32_t ref()
00329 {
00330 ++refcount;
00331 return refcount;
00332 }
00333
00338 inline int32_t ref_count() const
00339 {
00340 return refcount;
00341 }
00342
00348 inline int32_t unref()
00349 {
00350 if (refcount==0 || --refcount==0)
00351 {
00352 delete this;
00353 return 0;
00354 }
00355 else
00356 return refcount;
00357 }
00358
00360 inline const char* get_name() { return "IO"; }
00361
00362 protected:
00369 const char* get_msg_intro(EMessageType prio) const;
00370
00371 protected:
00373 FILE* target;
00375 float64_t last_progress_time;
00377 float64_t progress_start_time;
00379 float64_t last_progress;
00381 bool show_progress;
00384 bool show_file_and_line;
00385
00387 EMessageType loglevel;
00389 static const EMessageType levels[NUM_LOG_LEVELS];
00391 static const char* message_strings[NUM_LOG_LEVELS];
00392
00394 static char file_buffer[FBUFSIZE];
00396 static char directory_name[FBUFSIZE];
00397
00398 private:
00399 int32_t refcount;
00400 };
00401 }
00402 #endif // __CIO_H__