SHOGUN  6.1.3
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_GPL_ONLY { io->gpl_only(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
140 #define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
141 
142 #define SG_DONE() { \
143  if (SG_UNLIKELY(io->get_show_progress())) \
144  io->done(); \
145 }
146 
147 // printf like function using the global sg_io object
148 #define SG_SGCDEBUG(...) { \
149  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_GCDEBUG))) \
150  sg_io->message(MSG_GCDEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);\
151 }
152 
153 #define SG_SDEBUG(...) { \
154  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_DEBUG))) \
155  sg_io->message(MSG_DEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
156 }
157 
158 #define SG_SINFO(...) { \
159  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_INFO))) \
160  sg_io->message(MSG_INFO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
161 }
162 
163 #define SG_SWARNING(...) { sg_io->message(MSG_WARN,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
164 #define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
165 #define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
166 
167 #define SG_SDONE() { \
168  if (SG_UNLIKELY(sg_io->get_show_progress())) \
169  sg_io->done(); \
170 }
171 
172 #define SG_SNOTIMPLEMENTED { sg_io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
173 #define SG_SGPL_ONLY { sg_io->gpl_only(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
174 #define SG_SDEPRECATED { sg_io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
175 
176 #define ASSERT(x) { \
177  if (SG_UNLIKELY(!(x))) \
178  SG_SERROR("assertion %s failed in %s file %s line %d\n",#x, __PRETTY_FUNCTION__, __FILE__, __LINE__) \
179 }
180 
181 #define REQUIRE(x, ...) { \
182  if (SG_UNLIKELY(!(x))) \
183  SG_SERROR(__VA_ARGS__) \
184 }
185 
186 /* help clang static analyzer to identify custom assertation functions */
187 #ifdef __clang_analyzer__
188 void _clang_fail(void) __attribute__((analyzer_noreturn));
189 
190 #undef SG_ERROR(...)
191 #undef SG_SERROR(...)
192 #define SG_ERROR(...) _clang_fail();
193 #define SG_SERROR(...) _clang_fail();
194 
195 #endif /* __clang_analyzer__ */
196 
204 struct substring
205 {
207  char *start;
209  char *end;
210 };
211 
218 class SGIO
219 {
220  public:
222  SGIO();
224  SGIO(const SGIO& orig);
225 
227  virtual ~SGIO();
228 
233  void set_loglevel(EMessageType level);
234 
239  EMessageType get_loglevel() const;
240 
246  inline bool loglevel_above(EMessageType type) const
247  {
248  return loglevel <= type;
249  }
250 
255  inline bool get_show_progress() const
256  {
257  return show_progress;
258  }
259 
263  {
264  return location_info;
265  }
266 
271  inline bool get_syntax_highlight() const
272  {
273  return syntax_highlight;
274  }
275 
287  void message(EMessageType prio, const char* function, const char* file,
288  int32_t line, const char *fmt, ... ) const;
289 
294  void done();
295 
297  inline void not_implemented(const char* function, const char* file, int32_t line) const
298  {
299  message(MSG_ERROR, function, file, line, "Sorry, not yet implemented .\n");
300  }
301 
303  inline void gpl_only(const char* function, const char* file, int32_t line) const
304  {
305  message(MSG_ERROR, function, file, line, "This feature is only available if Shogun is built with GPL codes.\n");
306  }
307 
309  inline void deprecated(const char* function, const char* file, int32_t line) const
310  {
311  message(MSG_WARN, function, file, line,
312  "This function is deprecated and will be removed soon.\n");
313  }
314 
320  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
321 
327  static char* skip_spaces(char* str);
328 
334  static char* skip_blanks(char* str);
335 
340  inline FILE* get_target() const
341  {
342  return target;
343  }
344 
349  void set_target(FILE* target);
350 
352  inline void set_target_to_stderr() { set_target(stderr); }
353 
355  inline void set_target_to_stdout() { set_target(stdout); }
356 
358  inline void enable_progress()
359  {
360  show_progress=true;
361 
362  // static functions like CSVM::classify_example_helper call SG_PROGRESS
363  if (sg_io!=this)
365  }
366 
368  inline void disable_progress()
369  {
370  show_progress=false;
371 
372  // static functions like CSVM::classify_example_helper call SG_PROGRESS
373  if (sg_io!=this)
375  }
376 
382  inline void set_location_info(EMessageLocation location)
383  {
384  location_info = location;
385 
386  if (sg_io!=this)
387  sg_io->set_location_info(location);
388  }
389 
392  {
393  syntax_highlight=true;
394 
395  if (sg_io!=this)
397  }
398 
401  {
402  syntax_highlight=false;
403 
404  if (sg_io!=this)
406  }
407 
412  static inline void set_dirname(const char* dirname)
413  {
414 #ifdef _MSC_VER
415  strncpy_s(directory_name, FBUFSIZE, dirname, strlen(dirname));
416 #else
417  strncpy(directory_name, dirname, FBUFSIZE);
418 #endif
419  }
420 
427  static char* concat_filename(const char* filename);
428 
434  static int filter(CONST_DIRENT_T* d);
435 
441  static char* c_string_of_substring(substring s);
442 
447  static void print_substring(substring s);
448 
455  static float32_t float_of_substring(substring s);
456 
462  static float64_t double_of_substring(substring s);
463 
469  static int32_t int_of_substring(substring s);
470 
476  static uint32_t ulong_of_substring(substring s);
477 
483  static uint32_t ss_length(substring s);
484 
489  int32_t ref();
490 
495  int32_t ref_count() const;
496 
502  int32_t unref();
503 
505  inline const char* get_name() { return "SGIO"; }
506 
507  protected:
514  const char* get_msg_intro(EMessageType prio) const;
515 
516  protected:
518  FILE* target;
526 
530  static const EMessageType levels[NUM_LOG_LEVELS];
532  static const char* message_strings_highlighted[NUM_LOG_LEVELS];
534  static const char* message_strings[NUM_LOG_LEVELS];
535 
537  static char file_buffer[FBUFSIZE];
539  static char directory_name[FBUFSIZE];
540 
541  private:
542  RefCount* m_refcount;
543 };
544 }
545 #endif // __SGIO_H__
FILE * get_target() const
Definition: SGIO.h:340
char * end
Definition: SGIO.h:209
#define NUM_LOG_LEVELS
Definition: SGIO.h:67
char * start
Definition: SGIO.h:207
bool loglevel_above(EMessageType type) const
Definition: SGIO.h:246
struct Substring, specified by start position and end position.
Definition: SGIO.h:204
void disable_syntax_highlighting()
Definition: SGIO.h:400
#define FBUFSIZE
Definition: SGIO.h:68
SGIO * sg_io
Definition: init.cpp:44
FILE * target
Definition: SGIO.h:518
void disable_progress()
Definition: SGIO.h:368
void enable_syntax_highlighting()
Definition: SGIO.h:391
double float64_t
Definition: common.h:60
void set_target_to_stdout()
Definition: SGIO.h:355
void not_implemented(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:297
void gpl_only(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:303
#define CONST_DIRENT_T
Definition: SGIO.h:81
float float32_t
Definition: common.h:59
EMessageType
Definition: SGIO.h:43
bool get_show_progress() const
Definition: SGIO.h:255
EMessageLocation location_info
Definition: SGIO.h:523
static void set_dirname(const char *dirname)
Definition: SGIO.h:412
EMessageType loglevel
Definition: SGIO.h:528
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
const char * get_name()
Definition: SGIO.h:505
EMessageLocation
Definition: SGIO.h:60
void deprecated(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:309
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:218
bool show_progress
Definition: SGIO.h:520
bool syntax_highlight
Definition: SGIO.h:525
void set_location_info(EMessageLocation location)
Definition: SGIO.h:382
void set_target_to_stderr()
Definition: SGIO.h:352
bool get_syntax_highlight() const
Definition: SGIO.h:271
EMessageLocation get_location_info() const
Definition: SGIO.h:262
void enable_progress()
Definition: SGIO.h:358

SHOGUN Machine Learning Toolbox - Documentation