SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 #include <shogun/lib/common.h>
18 
19 #include <dirent.h>
20 #include <string.h>
21 #include <locale.h>
22 #include <sys/types.h>
23 
24 #ifndef _WIN32
25 #include <unistd.h>
26 #endif
27 
28 namespace shogun
29 {
30  class RefCount;
31  class SGIO;
33  extern SGIO* sg_io;
34 }
35 
36 
37 namespace shogun
38 {
44 {
55 };
56 
61 {
65 };
66 
67 #define NUM_LOG_LEVELS 10
68 #define FBUFSIZE 4096
69 
70 #ifdef DARWIN
71 #include <Availability.h>
72 #ifndef __MAC_10_8
73 #define __MAC_10_8 1080
74 #endif
75 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
76 #define CONST_DIRENT_T struct dirent
77 #else
78 #define CONST_DIRENT_T const struct dirent
79 #endif // Lion or earlier
80 #else //DARWIN
81 #define CONST_DIRENT_T const struct dirent
82 #endif //DARWIN
83 
84 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
85 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
86 
87 #if !defined(SG_UNLIKELY)
88 #if __GNUC__ >= 3
89 #define SG_UNLIKELY(expr) __builtin_expect(expr, 0)
90 #else
91 #define SG_UNLIKELY(expr) expr
92 #endif
93 #endif
94 
95 #ifdef _WIN32
96 #define __PRETTY_FUNCTION__ __FUNCTION__
97 #endif
98 
99 // printf like functions (with additional severity level)
100 // for object derived from CSGObject
101 #define SG_GCDEBUG(...) { \
102  if (SG_UNLIKELY(io->loglevel_above(MSG_GCDEBUG))) \
103  io->message(MSG_GCDEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
104 }
105 
106 #define SG_DEBUG(...) { \
107  if (SG_UNLIKELY(io->loglevel_above(MSG_DEBUG))) \
108  io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
109 }
110 
111 #define SG_OBJ_DEBUG(o,...) { \
112  if (SG_UNLIKELY(o->io->loglevel_above(MSG_DEBUG))) \
113  o->io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
114 }
115 
116 
117 #define SG_INFO(...) { \
118  if (SG_UNLIKELY(io->loglevel_above(MSG_INFO))) \
119  io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
120 }
121 
122 #define SG_CLASS_INFO(c, ...) { \
123  if (SG_UNLIKELY(c::io->loglevel_above(MSG_INFO))) \
124  c::io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
125 }
126 
127 #define SG_WARNING(...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
128 #define SG_ERROR(...) { io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
129 #define SG_OBJ_ERROR(o, ...) { o->io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
130 #define SG_CLASS_ERROR(c, ...) { c::io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
131 #define SG_UNSTABLE(func, ...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
132 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
133 "work or not to the Shogun mailing list. Thanking you in " \
134 "anticipation. " __VA_ARGS__); }
135 
136 #define SG_PRINT(...) { io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
137 #define SG_OBJ_PRINT(o, ...) { o->io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
138 #define SG_NOTIMPLEMENTED { io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
139 #define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
140 
141 #define SG_PROGRESS(...) { \
142  if (SG_UNLIKELY(io->get_show_progress())) \
143  io->progress(__VA_ARGS__); \
144 }
145 
146 #define SG_OBJ_PROGRESS(o, ...) { \
147  if (SG_UNLIKELY(o->io->get_show_progress()))\
148  o->io->progress(__VA_ARGS__); \
149 }
150 
151 #define SG_ABS_PROGRESS(...) { \
152  if (SG_UNLIKELY(io->get_show_progress())) \
153  io->absolute_progress(__VA_ARGS__); \
154 }
155 
156 #define SG_DONE() { \
157  if (SG_UNLIKELY(io->get_show_progress())) \
158  io->done(); \
159 }
160 
161 // printf like function using the global sg_io object
162 #define SG_SGCDEBUG(...) { \
163  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_GCDEBUG))) \
164  sg_io->message(MSG_GCDEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);\
165 }
166 
167 #define SG_SDEBUG(...) { \
168  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_DEBUG))) \
169  sg_io->message(MSG_DEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
170 }
171 
172 #define SG_SINFO(...) { \
173  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_INFO))) \
174  sg_io->message(MSG_INFO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
175 }
176 
177 #define SG_SWARNING(...) { sg_io->message(MSG_WARN,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
178 #define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
179 #define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
180 
181 
182 #define SG_SPROGRESS(...) { \
183  if (SG_UNLIKELY(sg_io->get_show_progress())) \
184  sg_io->progress(__VA_ARGS__); \
185 }
186 
187 #define SG_SABS_PROGRESS(...) { \
188  if (SG_UNLIKELY(sg_io->get_show_progress())) \
189  sg_io->absolute_progress(__VA_ARGS__); \
190 }
191 
192 #define SG_SDONE() { \
193  if (SG_UNLIKELY(sg_io->get_show_progress())) \
194  sg_io->done(); \
195 }
196 
197 #define SG_SNOTIMPLEMENTED { sg_io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
198 #define SG_SDEPRECATED { sg_io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
199 
200 #define ASSERT(x) { \
201  if (SG_UNLIKELY(!(x))) \
202  SG_SERROR("assertion %s failed in %s file %s line %d\n",#x, __PRETTY_FUNCTION__, __FILE__, __LINE__) \
203 }
204 
205 #define REQUIRE(x, ...) { \
206  if (SG_UNLIKELY(!(x))) \
207  SG_SERROR(__VA_ARGS__) \
208 }
209 
210 /* help clang static analyzer to identify custom assertation functions */
211 #ifdef __clang_analyzer__
212 void _clang_fail(void) __attribute__((analyzer_noreturn));
213 
214 #undef SG_ERROR(...)
215 #undef SG_SERROR(...)
216 #define SG_ERROR(...) _clang_fail();
217 #define SG_SERROR(...) _clang_fail();
218 
219 #endif /* __clang_analyzer__ */
220 
228 struct substring
229 {
231  char *start;
233  char *end;
234 };
235 
242 class SGIO
243 {
244  public:
246  SGIO();
248  SGIO(const SGIO& orig);
249 
251  virtual ~SGIO();
252 
257  void set_loglevel(EMessageType level);
258 
263  EMessageType get_loglevel() const;
264 
270  inline bool loglevel_above(EMessageType type) const
271  {
272  return loglevel <= type;
273  }
274 
279  inline bool get_show_progress() const
280  {
281  return show_progress;
282  }
283 
287  {
288  return location_info;
289  }
290 
293  {
294  return last_progress;
295  }
296 
301  inline bool get_syntax_highlight() const
302  {
303  return syntax_highlight;
304  }
305 
317  void message(EMessageType prio, const char* function, const char* file,
318  int32_t line, const char *fmt, ... ) const;
319 
328  void progress(
329  float64_t current_val,
330  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
331  const char* prefix="PROGRESS:\t");
332 
342  void absolute_progress(
343  float64_t current_val, float64_t val,
344  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
345  const char* prefix="PROGRESS:\t");
346 
351  void done();
352 
354  inline void not_implemented(const char* function, const char* file, int32_t line) const
355  {
356  message(MSG_ERROR, function, file, line, "Sorry, not yet implemented .\n");
357  }
358 
360  inline void deprecated(const char* function, const char* file, int32_t line) const
361  {
362  message(MSG_WARN, function, file, line,
363  "This function is deprecated and will be removed soon.\n");
364  }
365 
371  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
372 
378  static char* skip_spaces(char* str);
379 
385  static char* skip_blanks(char* str);
386 
391  inline FILE* get_target() const
392  {
393  return target;
394  }
395 
400  void set_target(FILE* target);
401 
403  inline void set_target_to_stderr() { set_target(stderr); }
404 
406  inline void set_target_to_stdout() { set_target(stdout); }
407 
409  inline void enable_progress()
410  {
411  show_progress=true;
412 
413  // static functions like CSVM::classify_example_helper call SG_PROGRESS
414  if (sg_io!=this)
416  }
417 
419  inline void disable_progress()
420  {
421  show_progress=false;
422 
423  // static functions like CSVM::classify_example_helper call SG_PROGRESS
424  if (sg_io!=this)
426  }
427 
433  inline void set_location_info(EMessageLocation location)
434  {
435  location_info = location;
436 
437  if (sg_io!=this)
438  sg_io->set_location_info(location);
439  }
440 
443  {
444  syntax_highlight=true;
445 
446  if (sg_io!=this)
448  }
449 
452  {
453  syntax_highlight=false;
454 
455  if (sg_io!=this)
457  }
458 
463  static inline void set_dirname(const char* dirname)
464  {
465 #ifdef _MSC_VER
466  strncpy_s(directory_name, FBUFSIZE, dirname, strlen(dirname));
467 #else
468  strncpy(directory_name, dirname, FBUFSIZE);
469 #endif
470  }
471 
478  static char* concat_filename(const char* filename);
479 
485  static int filter(CONST_DIRENT_T* d);
486 
492  static char* c_string_of_substring(substring s);
493 
498  static void print_substring(substring s);
499 
507 
514 
520  static int32_t int_of_substring(substring s);
521 
527  static uint32_t ulong_of_substring(substring s);
528 
534  static uint32_t ss_length(substring s);
535 
540  int32_t ref();
541 
546  int32_t ref_count() const;
547 
553  int32_t unref();
554 
556  inline const char* get_name() { return "SGIO"; }
557 
558  protected:
565  const char* get_msg_intro(EMessageType prio) const;
566 
567  protected:
569  FILE* target;
583 
591  static const char* message_strings[NUM_LOG_LEVELS];
592 
594  static char file_buffer[FBUFSIZE];
596  static char directory_name[FBUFSIZE];
597 
598  private:
599  RefCount* m_refcount;
600 };
601 }
602 #endif // __SGIO_H__
int32_t unref()
Definition: SGIO.cpp:450
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:320
virtual ~SGIO()
Definition: SGIO.cpp:435
static char * skip_spaces(char *str)
Definition: SGIO.cpp:287
FILE * get_target() const
Definition: SGIO.h:391
int32_t ref_count() const
Definition: SGIO.cpp:445
static uint32_t ss_length(substring s)
Definition: SGIO.cpp:400
void buffered_message(EMessageType prio, const char *fmt,...) const
Definition: SGIO.cpp:141
char * end
Definition: SGIO.h:233
#define NUM_LOG_LEVELS
Definition: SGIO.h:67
void set_target(FILE *target)
Definition: SGIO.cpp:325
float64_t last_progress
Definition: SGIO.h:575
char * start
Definition: SGIO.h:231
bool loglevel_above(EMessageType type) const
Definition: SGIO.h:270
static char * skip_blanks(char *str)
Definition: SGIO.cpp:301
static int filter(CONST_DIRENT_T *d)
Definition: SGIO.cpp:418
struct Substring, specified by start position and end position.
Definition: SGIO.h:228
void disable_syntax_highlighting()
Definition: SGIO.h:451
static char file_buffer[FBUFSIZE]
file name buffer
Definition: SGIO.h:594
#define FBUFSIZE
Definition: SGIO.h:68
SGIO * sg_io
Definition: init.cpp:38
void message(EMessageType prio, const char *function, const char *file, int32_t line, const char *fmt,...) const
Definition: SGIO.cpp:74
FILE * target
Definition: SGIO.h:569
void disable_progress()
Definition: SGIO.h:419
void enable_syntax_highlighting()
Definition: SGIO.h:442
static char * c_string_of_substring(substring s)
Definition: SGIO.cpp:351
float64_t last_progress_time
Definition: SGIO.h:571
double float64_t
Definition: common.h:60
int32_t ref()
Definition: SGIO.cpp:440
static float32_t float_of_substring(substring s)
Definition: SGIO.cpp:366
void set_target_to_stdout()
Definition: SGIO.h:406
static uint32_t ulong_of_substring(substring s)
Definition: SGIO.cpp:395
static const char * message_strings[NUM_LOG_LEVELS]
Definition: SGIO.h:591
static void print_substring(substring s)
Definition: SGIO.cpp:359
EMessageType get_loglevel() const
Definition: SGIO.cpp:315
void not_implemented(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:354
#define CONST_DIRENT_T
Definition: SGIO.h:81
float64_t progress_start_time
Definition: SGIO.h:573
float float32_t
Definition: common.h:59
static int32_t int_of_substring(substring s)
Definition: SGIO.cpp:386
EMessageType
Definition: SGIO.h:43
bool get_show_progress() const
Definition: SGIO.h:279
EMessageLocation location_info
Definition: SGIO.h:580
static void set_dirname(const char *dirname)
Definition: SGIO.h:463
EMessageType loglevel
Definition: SGIO.h:585
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:330
const char * get_name()
Definition: SGIO.h:556
static char directory_name[FBUFSIZE]
directory name buffer
Definition: SGIO.h:596
EMessageLocation
Definition: SGIO.h:60
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:155
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:228
static char * concat_filename(const char *filename)
Definition: SGIO.cpp:405
void deprecated(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:360
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:242
bool show_progress
Definition: SGIO.h:577
bool syntax_highlight
Definition: SGIO.h:582
void set_location_info(EMessageLocation location)
Definition: SGIO.h:433
void set_target_to_stderr()
Definition: SGIO.h:403
static float64_t double_of_substring(substring s)
Definition: SGIO.cpp:376
static const EMessageType levels[NUM_LOG_LEVELS]
Definition: SGIO.h:587
float64_t get_last_progress() const
Definition: SGIO.h:292
bool get_syntax_highlight() const
Definition: SGIO.h:301
static const char * message_strings_highlighted[NUM_LOG_LEVELS]
Definition: SGIO.h:589
void done()
Definition: SGIO.cpp:279
EMessageLocation get_location_info() const
Definition: SGIO.h:286
void enable_progress()
Definition: SGIO.h:409

SHOGUN Machine Learning Toolbox - Documentation