io.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef __CIO_H__
00013 #define __CIO_H__
00014 
00015 #include <time.h>
00016 #include "lib/common.h"
00017 
00018 #include <stdio.h>
00019 #include <stdarg.h>
00020 #include <string.h>
00021 
00022 #define NUM_LOG_LEVELS 9
00023 #define FBUFSIZE 4096
00024 
00025 #ifdef DARWIN
00026 #define CONST_DIRENT_T struct dirent
00027 #else //DARWIN
00028 #define CONST_DIRENT_T const struct dirent
00029 #endif //DARWIN
00030 
00031 extern CHAR file_buffer[FBUFSIZE];
00032 extern CHAR directory_name[FBUFSIZE];
00033 
00034 class CIO;
00035 
00036 // printf like funktions (with additional severity level)
00037 // for object derived from CSGObject
00038 #define SG_DEBUG(x...) CSGObject::io.message(M_DEBUG,x)
00039 #define SG_INFO(x...) CSGObject::io.message(M_INFO,x)
00040 #define SG_WARNING(x...) CSGObject::io.message(M_WARN,x)
00041 #define SG_ERROR(x...) CSGObject::io.message(M_ERROR,x)
00042 #define SG_PRINT(x...) CSGObject::io.message(M_MESSAGEONLY,x)
00043 #define SG_NOTIMPLEMENTED CSGObject::io.not_implemented()
00044 
00045 #define SG_PROGRESS(x...) CSGObject::io.progress(x)
00046 #define SG_ABS_PROGRESS(x...) CSGObject::io.absolute_progress(x)
00047 #define SG_DONE() CSGObject::io.done()
00048 
00049 #ifndef HAVE_SWIG
00050 extern CIO* sg_io;
00051 // printf like function using the global sg_io object
00052 #define SG_SDEBUG(x...) sg_io->message(M_DEBUG,x)
00053 #define SG_SINFO(x...) sg_io->message(M_INFO,x)
00054 #define SG_SWARNING(x...) sg_io->message(M_WARN,x)
00055 #define SG_SERROR(x...) sg_io->message(M_ERROR,x)
00056 #define SG_SPRINT(x...) sg_io->message(M_MESSAGEONLY,x)
00057 #define SG_SPROGRESS(x...) sg_io->progress(x)
00058 #define SG_SABS_PROGRESS(x...) sg_io->absolute_progress(x)
00059 #define SG_SDONE() sg_io->done()
00060 #define SG_SNOTIMPLEMENTED sg_io->not_implemented()
00061 #else
00062 extern CIO sg_io;
00063 // printf like function using the global sg_io object
00064 #define SG_SDEBUG(x...) sg_io.message(M_DEBUG,x)
00065 #define SG_SINFO(x...) sg_io.message(M_INFO,x)
00066 #define SG_SWARNING(x...) sg_io.message(M_WARN,x)
00067 #define SG_SERROR(x...) sg_io.message(M_ERROR,x)
00068 #define SG_SPRINT(x...) sg_io.message(M_MESSAGEONLY,x)
00069 #define SG_SPROGRESS(x...) sg_io.progress(x)
00070 #define SG_SABS_PROGRESS(x...) sg_io.absolute_progress(x)
00071 #define SG_SDONE() sg_io.done()
00072 #define SG_SNOTIMPLEMENTED sg_io.not_implemented()
00073 #endif
00074 
00075 #define ASSERT(x) { if (!(x)) SG_SERROR("assertion %s failed in file %s line %d\n",#x, __FILE__, __LINE__);}
00076 
00078 class CIO
00079 {
00080     public:
00082         CIO();
00084         CIO(const CIO& orig);
00085 
00090         void set_loglevel(EMessageType level);
00091 
00096         EMessageType get_loglevel() const;
00097 
00102         bool get_show_progress() const;
00103 
00109         void message(EMessageType prio, const char *fmt, ... ) const;
00110 
00119         void progress(DREAL current_val, DREAL min_val=0.0, DREAL max_val=1.0, INT decimals=1, const char* prefix="PROGRESS:\t");
00120 
00130         void absolute_progress(DREAL current_val, DREAL val, DREAL min_val=0.0, DREAL max_val=1.0, INT decimals=1, const char* prefix="PROGRESS:\t");
00131 
00136         void done();
00137 
00139         inline void not_implemented() const
00140         {
00141             message(M_ERROR, "Sorry, not yet implemented\n");
00142         }
00143 
00149         void buffered_message(EMessageType prio, const CHAR *fmt, ... ) const;
00150 
00156         static CHAR* skip_spaces(CHAR* str);
00157 
00163         static CHAR* skip_blanks(CHAR* str);
00164 
00169         inline FILE* get_target() const
00170         {
00171             return target;
00172         }
00173 
00178         void set_target(FILE* target);
00179 
00181         inline void set_target_to_stderr() { set_target(stderr); }
00182 
00184         inline void set_target_to_stdout() { set_target(stdout); }
00185 
00187         inline void enable_progress()
00188         {
00189             show_progress=true;
00190 
00191             // static functions like CSVM::classify_example_helper call SG_PROGRESS
00192 #ifndef HAVE_SWIG
00193             if (sg_io!=this)
00194                 sg_io->enable_progress();
00195 #else
00196             if (&sg_io!=this)
00197                 sg_io.enable_progress();
00198 #endif
00199         }
00200 
00202         inline void disable_progress()
00203         {
00204             show_progress=false;
00205 
00206             // static functions like CSVM::classify_example_helper call SG_PROGRESS
00207 #ifndef HAVE_SWIG
00208             if (sg_io!=this)
00209                 sg_io->disable_progress();
00210 #else
00211             if (&sg_io!=this)
00212                 sg_io.disable_progress();
00213 #endif
00214         }
00215 
00220         inline void set_dirname(const CHAR* dirname)
00221         {
00222             strncpy(directory_name, dirname, FBUFSIZE);
00223         }
00224 
00231         static CHAR* concat_filename(const CHAR* filename);
00232 
00238         static int filter(CONST_DIRENT_T* d);
00239 
00240     protected:
00247         const CHAR* get_msg_intro(EMessageType prio) const;
00248 
00249     protected:
00251         FILE* target;
00253         LONG last_progress_time;
00255         LONG progress_start_time;
00257         DREAL last_progress;
00259         bool show_progress;
00260 
00262         EMessageType loglevel;
00264         static const EMessageType levels[NUM_LOG_LEVELS];
00266         static const char* message_strings[NUM_LOG_LEVELS];
00267 };
00268 
00269 #endif // __CIO_H__

SHOGUN Machine Learning Toolbox - Documentation