io.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __CIO_H__
00013 #define __CIO_H__
00014
00015 #include <time.h>
00016 #include <stdio.h>
00017 #include <stdarg.h>
00018 #include <string.h>
00019
00020 #include "lib/common.h"
00021
00022
00026 enum EMessageType
00027 {
00028 M_DEBUG,
00029 M_INFO,
00030 M_NOTICE,
00031 M_WARN,
00032 M_ERROR,
00033 M_CRITICAL,
00034 M_ALERT,
00035 M_EMERGENCY,
00036 M_MESSAGEONLY
00037 };
00038
00039
00040 #define NUM_LOG_LEVELS 9
00041 #define FBUFSIZE 4096
00042
00043 #ifdef DARWIN
00044 #define CONST_DIRENT_T struct dirent
00045 #else //DARWIN
00046 #define CONST_DIRENT_T const struct dirent
00047 #endif //DARWIN
00048
00049 extern char file_buffer[FBUFSIZE];
00050 extern char directory_name[FBUFSIZE];
00051
00052 class CIO;
00053
00054
00055
00056 #define SG_DEBUG(x...) CSGObject::io.message(M_DEBUG,x)
00057 #define SG_INFO(x...) CSGObject::io.message(M_INFO,x)
00058 #define SG_WARNING(x...) CSGObject::io.message(M_WARN,x)
00059 #define SG_ERROR(x...) CSGObject::io.message(M_ERROR,x)
00060 #define SG_PRINT(x...) CSGObject::io.message(M_MESSAGEONLY,x)
00061 #define SG_NOTIMPLEMENTED CSGObject::io.not_implemented()
00062
00063 #define SG_PROGRESS(x...) CSGObject::io.progress(x)
00064 #define SG_ABS_PROGRESS(x...) CSGObject::io.absolute_progress(x)
00065 #define SG_DONE() CSGObject::io.done()
00066
00067 #ifndef HAVE_SWIG
00068 extern CIO* sg_io;
00069
00070 #define SG_SDEBUG(x...) sg_io->message(M_DEBUG,x)
00071 #define SG_SINFO(x...) sg_io->message(M_INFO,x)
00072 #define SG_SWARNING(x...) sg_io->message(M_WARN,x)
00073 #define SG_SERROR(x...) sg_io->message(M_ERROR,x)
00074 #define SG_SPRINT(x...) sg_io->message(M_MESSAGEONLY,x)
00075 #define SG_SPROGRESS(x...) sg_io->progress(x)
00076 #define SG_SABS_PROGRESS(x...) sg_io->absolute_progress(x)
00077 #define SG_SDONE() sg_io->done()
00078 #define SG_SNOTIMPLEMENTED sg_io->not_implemented()
00079 #else
00080 extern CIO sg_io;
00081
00082 #define SG_SDEBUG(x...) sg_io.message(M_DEBUG,x)
00083 #define SG_SINFO(x...) sg_io.message(M_INFO,x)
00084 #define SG_SWARNING(x...) sg_io.message(M_WARN,x)
00085 #define SG_SERROR(x...) sg_io.message(M_ERROR,x)
00086 #define SG_SPRINT(x...) sg_io.message(M_MESSAGEONLY,x)
00087 #define SG_SPROGRESS(x...) sg_io.progress(x)
00088 #define SG_SABS_PROGRESS(x...) sg_io.absolute_progress(x)
00089 #define SG_SDONE() sg_io.done()
00090 #define SG_SNOTIMPLEMENTED sg_io.not_implemented()
00091 #endif
00092
00093 #define ASSERT(x) { if (!(x)) SG_SERROR("assertion %s failed in file %s line %d\n",#x, __FILE__, __LINE__);}
00094
00095
00101 class CIO
00102 {
00103 public:
00105 CIO();
00107 CIO(const CIO& orig);
00108
00113 void set_loglevel(EMessageType level);
00114
00119 EMessageType get_loglevel() const;
00120
00125 bool get_show_progress() const;
00126
00132 void message(EMessageType prio, const char *fmt, ... ) const;
00133
00142 void progress(
00143 float64_t current_val,
00144 float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
00145 const char* prefix="PROGRESS:\t");
00146
00156 void absolute_progress(
00157 float64_t current_val, float64_t val,
00158 float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
00159 const char* prefix="PROGRESS:\t");
00160
00165 void done();
00166
00168 inline void not_implemented() const
00169 {
00170 message(M_ERROR, "Sorry, not yet implemented\n");
00171 }
00172
00178 void buffered_message(EMessageType prio, const char *fmt, ... ) const;
00179
00185 static char* skip_spaces(char* str);
00186
00192 static char* skip_blanks(char* str);
00193
00198 inline FILE* get_target() const
00199 {
00200 return target;
00201 }
00202
00207 void set_target(FILE* target);
00208
00210 inline void set_target_to_stderr() { set_target(stderr); }
00211
00213 inline void set_target_to_stdout() { set_target(stdout); }
00214
00216 inline void enable_progress()
00217 {
00218 show_progress=true;
00219
00220
00221 #ifndef HAVE_SWIG
00222 if (sg_io!=this)
00223 sg_io->enable_progress();
00224 #else
00225 if (&sg_io!=this)
00226 sg_io.enable_progress();
00227 #endif
00228 }
00229
00231 inline void disable_progress()
00232 {
00233 show_progress=false;
00234
00235
00236 #ifndef HAVE_SWIG
00237 if (sg_io!=this)
00238 sg_io->disable_progress();
00239 #else
00240 if (&sg_io!=this)
00241 sg_io.disable_progress();
00242 #endif
00243 }
00244
00249 inline void set_dirname(const char* dirname)
00250 {
00251 strncpy(directory_name, dirname, FBUFSIZE);
00252 }
00253
00260 static char* concat_filename(const char* filename);
00261
00267 static int filter(CONST_DIRENT_T* d);
00268
00269 protected:
00276 const char* get_msg_intro(EMessageType prio) const;
00277
00278 protected:
00280 FILE* target;
00282 int64_t last_progress_time;
00284 int64_t progress_start_time;
00286 float64_t last_progress;
00288 bool show_progress;
00289
00291 EMessageType loglevel;
00293 static const EMessageType levels[NUM_LOG_LEVELS];
00295 static const char* message_strings[NUM_LOG_LEVELS];
00296 };
00297
00298 #endif // __CIO_H__