SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SGIO.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2013 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  * Copyright (C) 2010-2013 Soeren Sonnenburg
11  */
12 
13 #ifndef __SGIO_H__
14 #define __SGIO_H__
15 
16 #include <shogun/lib/config.h>
17 
18 #include <dirent.h>
19 #include <string.h>
20 #include <locale.h>
21 #include <sys/types.h>
22 
23 #ifndef _WIN32
24 #include <unistd.h>
25 #endif
26 
27 #include <shogun/lib/common.h>
28 
29 namespace shogun
30 {
31  class RefCount;
32  class SGIO;
34  extern SGIO* sg_io;
35 }
36 
37 
38 namespace shogun
39 {
45 {
56 };
57 
62 {
66 };
67 
68 #define NUM_LOG_LEVELS 10
69 #define FBUFSIZE 4096
70 
71 #ifdef DARWIN
72 #include <Availability.h>
73 #ifndef __MAC_10_8
74 #define __MAC_10_8 1080
75 #endif
76 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
77 #define CONST_DIRENT_T struct dirent
78 #else
79 #define CONST_DIRENT_T const struct dirent
80 #endif // Lion or earlier
81 #else //DARWIN
82 #define CONST_DIRENT_T const struct dirent
83 #endif //DARWIN
84 
85 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
86 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
87 
88 #if !defined(SG_UNLIKELY)
89 #if __GNUC__ >= 3
90 #define SG_UNLIKELY(expr) __builtin_expect(expr, 0)
91 #else
92 #define SG_UNLIKELY(expr) expr
93 #endif
94 #endif
95 
96 #ifdef _WIN32
97 #define __PRETTY_FUNCTION__ __FUNCTION__
98 #endif
99 
100 // printf like functions (with additional severity level)
101 // for object derived from CSGObject
102 #define SG_GCDEBUG(...) { \
103  if (SG_UNLIKELY(io->loglevel_above(MSG_GCDEBUG))) \
104  io->message(MSG_GCDEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
105 }
106 
107 #define SG_DEBUG(...) { \
108  if (SG_UNLIKELY(io->loglevel_above(MSG_DEBUG))) \
109  io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
110 }
111 
112 #define SG_OBJ_DEBUG(o,...) { \
113  if (SG_UNLIKELY(o->io->loglevel_above(MSG_DEBUG))) \
114  o->io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
115 }
116 
117 
118 #define SG_INFO(...) { \
119  if (SG_UNLIKELY(io->loglevel_above(MSG_INFO))) \
120  io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
121 }
122 
123 #define SG_CLASS_INFO(c, ...) { \
124  if (SG_UNLIKELY(c::io->loglevel_above(MSG_INFO))) \
125  c::io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
126 }
127 
128 #define SG_WARNING(...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
129 #define SG_ERROR(...) { io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
130 #define SG_OBJ_ERROR(o, ...) { o->io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
131 #define SG_CLASS_ERROR(c, ...) { c::io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
132 #define SG_UNSTABLE(func, ...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
133 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
134 "work or not to the Shogun mailing list. Thanking you in " \
135 "anticipation. " __VA_ARGS__); }
136 
137 #define SG_PRINT(...) { io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
138 #define SG_OBJ_PRINT(o, ...) { o->io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
139 #define SG_NOTIMPLEMENTED { io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
140 #define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
141 
142 #define SG_PROGRESS(...) { \
143  if (SG_UNLIKELY(io->get_show_progress())) \
144  io->progress(__VA_ARGS__); \
145 }
146 
147 #define SG_OBJ_PROGRESS(o, ...) { \
148  if (SG_UNLIKELY(o->io->get_show_progress()))\
149  o->io->progress(__VA_ARGS__); \
150 }
151 
152 #define SG_ABS_PROGRESS(...) { \
153  if (SG_UNLIKELY(io->get_show_progress())) \
154  io->absolute_progress(__VA_ARGS__); \
155 }
156 
157 #define SG_DONE() { \
158  if (SG_UNLIKELY(io->get_show_progress())) \
159  io->done(); \
160 }
161 
162 // printf like function using the global sg_io object
163 #define SG_SGCDEBUG(...) { \
164  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_GCDEBUG))) \
165  sg_io->message(MSG_GCDEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);\
166 }
167 
168 #define SG_SDEBUG(...) { \
169  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_DEBUG))) \
170  sg_io->message(MSG_DEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
171 }
172 
173 #define SG_SINFO(...) { \
174  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_INFO))) \
175  sg_io->message(MSG_INFO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
176 }
177 
178 #define SG_SWARNING(...) { sg_io->message(MSG_WARN,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
179 #define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
180 #define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
181 
182 
183 #define SG_SPROGRESS(...) { \
184  if (SG_UNLIKELY(sg_io->get_show_progress())) \
185  sg_io->progress(__VA_ARGS__); \
186 }
187 
188 #define SG_SABS_PROGRESS(...) { \
189  if (SG_UNLIKELY(sg_io->get_show_progress())) \
190  sg_io->absolute_progress(__VA_ARGS__); \
191 }
192 
193 #define SG_SDONE() { \
194  if (SG_UNLIKELY(sg_io->get_show_progress())) \
195  sg_io->done(); \
196 }
197 
198 #define SG_SNOTIMPLEMENTED { sg_io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
199 #define SG_SDEPRECATED { sg_io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
200 
201 #define ASSERT(x) { \
202  if (SG_UNLIKELY(!(x))) \
203  SG_SERROR("assertion %s failed in %s file %s line %d\n",#x, __PRETTY_FUNCTION__, __FILE__, __LINE__) \
204 }
205 
206 #define REQUIRE(x, ...) { \
207  if (SG_UNLIKELY(!(x))) \
208  SG_SERROR(__VA_ARGS__) \
209 }
210 
211 /* help clang static analyzer to identify custom assertation functions */
212 #ifdef __clang_analyzer__
213 void _clang_fail(void) __attribute__((analyzer_noreturn));
214 
215 #undef SG_ERROR(...)
216 #undef SG_SERROR(...)
217 #define SG_ERROR(...) _clang_fail();
218 #define SG_SERROR(...) _clang_fail();
219 
220 #endif /* __clang_analyzer__ */
221 
229 struct substring
230 {
232  char *start;
234  char *end;
235 };
236 
243 class SGIO
244 {
245  public:
247  SGIO();
249  SGIO(const SGIO& orig);
250 
252  virtual ~SGIO();
253 
258  void set_loglevel(EMessageType level);
259 
264  EMessageType get_loglevel() const;
265 
271  inline bool loglevel_above(EMessageType type) const
272  {
273  return loglevel <= type;
274  }
275 
280  inline bool get_show_progress() const
281  {
282  return show_progress;
283  }
284 
288  {
289  return location_info;
290  }
291 
296  inline bool get_syntax_highlight() const
297  {
298  return syntax_highlight;
299  }
300 
312  void message(EMessageType prio, const char* function, const char* file,
313  int32_t line, const char *fmt, ... ) const;
314 
323  void progress(
324  float64_t current_val,
325  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
326  const char* prefix="PROGRESS:\t");
327 
337  void absolute_progress(
338  float64_t current_val, float64_t val,
339  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
340  const char* prefix="PROGRESS:\t");
341 
346  void done();
347 
349  inline void not_implemented(const char* function, const char* file, int32_t line) const
350  {
351  message(MSG_ERROR, function, file, line, "Sorry, not yet implemented .\n");
352  }
353 
355  inline void deprecated(const char* function, const char* file, int32_t line) const
356  {
357  message(MSG_WARN, function, file, line,
358  "This function is deprecated and will be removed soon.\n");
359  }
360 
366  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
367 
373  static char* skip_spaces(char* str);
374 
380  static char* skip_blanks(char* str);
381 
386  inline FILE* get_target() const
387  {
388  return target;
389  }
390 
395  void set_target(FILE* target);
396 
398  inline void set_target_to_stderr() { set_target(stderr); }
399 
401  inline void set_target_to_stdout() { set_target(stdout); }
402 
404  inline void enable_progress()
405  {
406  show_progress=true;
407 
408  // static functions like CSVM::classify_example_helper call SG_PROGRESS
409  if (sg_io!=this)
411  }
412 
414  inline void disable_progress()
415  {
416  show_progress=false;
417 
418  // static functions like CSVM::classify_example_helper call SG_PROGRESS
419  if (sg_io!=this)
421  }
422 
428  inline void set_location_info(EMessageLocation location)
429  {
430  location_info = location;
431 
432  if (sg_io!=this)
433  sg_io->set_location_info(location);
434  }
435 
438  {
439  syntax_highlight=true;
440 
441  if (sg_io!=this)
443  }
444 
447  {
448  syntax_highlight=false;
449 
450  if (sg_io!=this)
452  }
453 
458  static inline void set_dirname(const char* dirname)
459  {
460  strncpy(directory_name, dirname, FBUFSIZE);
461  }
462 
469  static char* concat_filename(const char* filename);
470 
476  static int filter(CONST_DIRENT_T* d);
477 
483  static char* c_string_of_substring(substring s);
484 
489  static void print_substring(substring s);
490 
498 
505 
511  static int32_t int_of_substring(substring s);
512 
518  static uint32_t ulong_of_substring(substring s);
519 
525  static uint32_t ss_length(substring s);
526 
531  int32_t ref();
532 
537  int32_t ref_count() const;
538 
544  int32_t unref();
545 
547  inline const char* get_name() { return "SGIO"; }
548 
549  protected:
556  const char* get_msg_intro(EMessageType prio) const;
557 
558  protected:
560  FILE* target;
574 
582  static const char* message_strings[NUM_LOG_LEVELS];
583 
585  static char file_buffer[FBUFSIZE];
587  static char directory_name[FBUFSIZE];
588 
589  private:
590  RefCount* m_refcount;
591 };
592 }
593 #endif // __SGIO_H__
int32_t unref()
Definition: SGIO.cpp:416
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:290
virtual ~SGIO()
Definition: SGIO.cpp:401
static char * skip_spaces(char *str)
Definition: SGIO.cpp:257
FILE * get_target() const
Definition: SGIO.h:386
int32_t ref_count() const
Definition: SGIO.cpp:411
static uint32_t ss_length(substring s)
Definition: SGIO.cpp:370
void buffered_message(EMessageType prio, const char *fmt,...) const
Definition: SGIO.cpp:133
char * end
Definition: SGIO.h:234
#define NUM_LOG_LEVELS
Definition: SGIO.h:68
void set_target(FILE *target)
Definition: SGIO.cpp:295
float64_t last_progress
Definition: SGIO.h:566
char * start
Definition: SGIO.h:232
bool loglevel_above(EMessageType type) const
Definition: SGIO.h:271
static char * skip_blanks(char *str)
Definition: SGIO.cpp:271
static int filter(CONST_DIRENT_T *d)
Definition: SGIO.cpp:384
struct Substring, specified by start position and end position.
Definition: SGIO.h:229
void disable_syntax_highlighting()
Definition: SGIO.h:446
static char file_buffer[FBUFSIZE]
file name buffer
Definition: SGIO.h:585
#define FBUFSIZE
Definition: SGIO.h:69
SGIO * sg_io
Definition: init.cpp:36
void message(EMessageType prio, const char *function, const char *file, int32_t line, const char *fmt,...) const
Definition: SGIO.cpp:66
FILE * target
Definition: SGIO.h:560
void disable_progress()
Definition: SGIO.h:414
void enable_syntax_highlighting()
Definition: SGIO.h:437
static char * c_string_of_substring(substring s)
Definition: SGIO.cpp:321
float64_t last_progress_time
Definition: SGIO.h:562
double float64_t
Definition: common.h:50
int32_t ref()
Definition: SGIO.cpp:406
static float32_t float_of_substring(substring s)
Definition: SGIO.cpp:336
void set_target_to_stdout()
Definition: SGIO.h:401
static uint32_t ulong_of_substring(substring s)
Definition: SGIO.cpp:365
static const char * message_strings[NUM_LOG_LEVELS]
Definition: SGIO.h:582
static void print_substring(substring s)
Definition: SGIO.cpp:329
EMessageType get_loglevel() const
Definition: SGIO.cpp:285
void not_implemented(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:349
#define CONST_DIRENT_T
Definition: SGIO.h:82
float64_t progress_start_time
Definition: SGIO.h:564
float float32_t
Definition: common.h:49
static int32_t int_of_substring(substring s)
Definition: SGIO.cpp:356
EMessageType
Definition: SGIO.h:44
bool get_show_progress() const
Definition: SGIO.h:280
EMessageLocation location_info
Definition: SGIO.h:571
static void set_dirname(const char *dirname)
Definition: SGIO.h:458
EMessageType loglevel
Definition: SGIO.h:576
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
const char * get_msg_intro(EMessageType prio) const
Definition: SGIO.cpp:300
const char * get_name()
Definition: SGIO.h:547
static char directory_name[FBUFSIZE]
directory name buffer
Definition: SGIO.h:587
EMessageLocation
Definition: SGIO.h:61
void progress(float64_t current_val, float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1, const char *prefix="PROGRESS:\t")
Definition: SGIO.cpp:147
void absolute_progress(float64_t current_val, float64_t val, float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1, const char *prefix="PROGRESS:\t")
Definition: SGIO.cpp:198
static char * concat_filename(const char *filename)
Definition: SGIO.cpp:375
void deprecated(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:355
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:243
bool show_progress
Definition: SGIO.h:568
bool syntax_highlight
Definition: SGIO.h:573
void set_location_info(EMessageLocation location)
Definition: SGIO.h:428
void set_target_to_stderr()
Definition: SGIO.h:398
static float64_t double_of_substring(substring s)
Definition: SGIO.cpp:346
static const EMessageType levels[NUM_LOG_LEVELS]
Definition: SGIO.h:578
bool get_syntax_highlight() const
Definition: SGIO.h:296
static const char * message_strings_highlighted[NUM_LOG_LEVELS]
Definition: SGIO.h:580
void done()
Definition: SGIO.cpp:249
EMessageLocation get_location_info() const
Definition: SGIO.h:287
void enable_progress()
Definition: SGIO.h:404

SHOGUN Machine Learning Toolbox - Documentation