SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 <stdio.h>
19 #include <string.h>
20 #include <dirent.h>
21 #include <locale.h>
22 #include <sys/types.h>
23 
24 #ifndef _WIN32
25 #include <unistd.h>
26 #endif
27 
28 #include <shogun/lib/common.h>
29 
30 namespace shogun
31 {
32  class RefCount;
33  class SGIO;
35  extern SGIO* sg_io;
36 }
37 
38 
39 namespace shogun
40 {
46 {
57 };
58 
63 {
67 };
68 
69 #define NUM_LOG_LEVELS 10
70 #define FBUFSIZE 4096
71 
72 #ifdef DARWIN
73 #include <Availability.h>
74 #ifndef __MAC_10_8
75 #define __MAC_10_8 1080
76 #endif
77 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
78 #define CONST_DIRENT_T struct dirent
79 #else
80 #define CONST_DIRENT_T const struct dirent
81 #endif // Lion or earlier
82 #else //DARWIN
83 #define CONST_DIRENT_T const struct dirent
84 #endif //DARWIN
85 
86 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
87 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
88 
89 #if !defined(SG_UNLIKELY)
90 #if __GNUC__ >= 3
91 #define SG_UNLIKELY(expr) __builtin_expect(expr, 0)
92 #else
93 #define SG_UNLIKELY(expr) expr
94 #endif
95 #endif
96 
97 #ifdef _WIN32
98 #define __PRETTY_FUNCTION__ __FUNCTION__
99 #endif
100 
101 // printf like funktions (with additional severity level)
102 // for object derived from CSGObject
103 #define SG_GCDEBUG(...) { \
104  if (SG_UNLIKELY(io->loglevel_above(MSG_GCDEBUG))) \
105  io->message(MSG_GCDEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
106 }
107 
108 #define SG_DEBUG(...) { \
109  if (SG_UNLIKELY(io->loglevel_above(MSG_DEBUG))) \
110  io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
111 }
112 
113 #define SG_OBJ_DEBUG(o,...) { \
114  if (SG_UNLIKELY(o->io->loglevel_above(MSG_DEBUG))) \
115  o->io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
116 }
117 
118 
119 #define SG_INFO(...) { \
120  if (SG_UNLIKELY(io->loglevel_above(MSG_INFO))) \
121  io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
122 }
123 
124 #define SG_CLASS_INFO(c, ...) { \
125  if (SG_UNLIKELY(c::io->loglevel_above(MSG_INFO))) \
126  c::io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
127 }
128 
129 #define SG_WARNING(...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
130 #define SG_ERROR(...) { io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
131 #define SG_OBJ_ERROR(o, ...) { o->io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
132 #define SG_CLASS_ERROR(c, ...) { c::io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
133 #define SG_UNSTABLE(func, ...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
134 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
135 "work or not to the Shogun mailing list. Thanking you in " \
136 "anticipation. " __VA_ARGS__); }
137 
138 #define SG_PRINT(...) { io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
139 #define SG_OBJ_PRINT(o, ...) { o->io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
140 #define SG_NOTIMPLEMENTED { io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
141 #define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
142 
143 #define SG_PROGRESS(...) { \
144  if (SG_UNLIKELY(io->get_show_progress())) \
145  io->progress(__VA_ARGS__); \
146 }
147 
148 #define SG_OBJ_PROGRESS(o, ...) { \
149  if (SG_UNLIKELY(o->io->get_show_progress()))\
150  o->io->progress(__VA_ARGS__); \
151 }
152 
153 #define SG_ABS_PROGRESS(...) { \
154  if (SG_UNLIKELY(io->get_show_progress())) \
155  io->absolute_progress(__VA_ARGS__); \
156 }
157 
158 #define SG_DONE() { \
159  if (SG_UNLIKELY(io->get_show_progress())) \
160  io->done(); \
161 }
162 
163 // printf like function using the global sg_io object
164 #define SG_SGCDEBUG(...) { \
165  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_GCDEBUG))) \
166  sg_io->message(MSG_GCDEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);\
167 }
168 
169 #define SG_SDEBUG(...) { \
170  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_DEBUG))) \
171  sg_io->message(MSG_DEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
172 }
173 
174 #define SG_SINFO(...) { \
175  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_INFO))) \
176  sg_io->message(MSG_INFO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
177 }
178 
179 #define SG_SWARNING(...) { sg_io->message(MSG_WARN,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
180 #define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
181 #define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
182 
183 
184 #define SG_SPROGRESS(...) { \
185  if (SG_UNLIKELY(sg_io->get_show_progress())) \
186  sg_io->progress(__VA_ARGS__); \
187 }
188 
189 #define SG_SABS_PROGRESS(...) { \
190  if (SG_UNLIKELY(sg_io->get_show_progress())) \
191  sg_io->absolute_progress(__VA_ARGS__); \
192 }
193 
194 #define SG_SDONE() { \
195  if (SG_UNLIKELY(sg_io->get_show_progress())) \
196  sg_io->done(); \
197 }
198 
199 #define SG_SNOTIMPLEMENTED { sg_io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
200 #define SG_SDEPRECATED { sg_io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
201 
202 #define ASSERT(x) { \
203  if (SG_UNLIKELY(!(x))) \
204  SG_SERROR("assertion %s failed in %s file %s line %d\n",#x, __PRETTY_FUNCTION__, __FILE__, __LINE__) \
205 }
206 
207 #define REQUIRE(x, ...) { \
208  if (SG_UNLIKELY(!(x))) \
209  SG_SERROR(__VA_ARGS__) \
210 }
211 
212 /* help clang static analyzer to identify custom assertation functions */
213 #ifdef __clang_analyzer__
214 void _clang_fail(void) __attribute__((analyzer_noreturn));
215 
216 #undef SG_ERROR(...)
217 #undef SG_SERROR(...)
218 #define SG_ERROR(...) _clang_fail();
219 #define SG_SERROR(...) _clang_fail();
220 
221 #endif /* __clang_analyzer__ */
222 
230 struct substring
231 {
233  char *start;
235  char *end;
236 };
237 
244 class SGIO
245 {
246  public:
248  SGIO();
250  SGIO(const SGIO& orig);
251 
253  virtual ~SGIO();
254 
259  void set_loglevel(EMessageType level);
260 
265  EMessageType get_loglevel() const;
266 
272  inline bool loglevel_above(EMessageType type) const
273  {
274  return loglevel <= type;
275  }
276 
281  inline bool get_show_progress() const
282  {
283  return show_progress;
284  }
285 
289  {
290  return location_info;
291  }
292 
297  inline bool get_syntax_highlight() const
298  {
299  return syntax_highlight;
300  }
301 
313  void message(EMessageType prio, const char* function, const char* file,
314  int32_t line, const char *fmt, ... ) const;
315 
324  void progress(
325  float64_t current_val,
326  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
327  const char* prefix="PROGRESS:\t");
328 
338  void absolute_progress(
339  float64_t current_val, float64_t val,
340  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
341  const char* prefix="PROGRESS:\t");
342 
347  void done();
348 
350  inline void not_implemented(const char* function, const char* file, int32_t line) const
351  {
352  message(MSG_ERROR, function, file, line, "Sorry, not yet implemented .\n");
353  }
354 
356  inline void deprecated(const char* function, const char* file, int32_t line) const
357  {
358  message(MSG_WARN, function, file, line,
359  "This function is deprecated and will be removed soon.\n");
360  }
361 
367  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
368 
374  static char* skip_spaces(char* str);
375 
381  static char* skip_blanks(char* str);
382 
387  inline FILE* get_target() const
388  {
389  return target;
390  }
391 
396  void set_target(FILE* target);
397 
399  inline void set_target_to_stderr() { set_target(stderr); }
400 
402  inline void set_target_to_stdout() { set_target(stdout); }
403 
405  inline void enable_progress()
406  {
407  show_progress=true;
408 
409  // static functions like CSVM::classify_example_helper call SG_PROGRESS
410  if (sg_io!=this)
412  }
413 
415  inline void disable_progress()
416  {
417  show_progress=false;
418 
419  // static functions like CSVM::classify_example_helper call SG_PROGRESS
420  if (sg_io!=this)
422  }
423 
429  inline void set_location_info(EMessageLocation location)
430  {
431  location_info = location;
432 
433  if (sg_io!=this)
434  sg_io->set_location_info(location);
435  }
436 
439  {
440  syntax_highlight=true;
441 
442  if (sg_io!=this)
444  }
445 
448  {
449  syntax_highlight=false;
450 
451  if (sg_io!=this)
453  }
454 
459  static inline void set_dirname(const char* dirname)
460  {
461  strncpy(directory_name, dirname, FBUFSIZE);
462  }
463 
470  static char* concat_filename(const char* filename);
471 
477  static int filter(CONST_DIRENT_T* d);
478 
484  static char* c_string_of_substring(substring s);
485 
490  static void print_substring(substring s);
491 
499 
506 
512  static int32_t int_of_substring(substring s);
513 
519  static uint32_t ulong_of_substring(substring s);
520 
526  static uint32_t ss_length(substring s);
527 
532  int32_t ref();
533 
538  int32_t ref_count() const;
539 
545  int32_t unref();
546 
548  inline const char* get_name() { return "SGIO"; }
549 
550  protected:
557  const char* get_msg_intro(EMessageType prio) const;
558 
559  protected:
561  FILE* target;
575 
583  static const char* message_strings[NUM_LOG_LEVELS];
584 
586  static char file_buffer[FBUFSIZE];
588  static char directory_name[FBUFSIZE];
589 
590  private:
591  RefCount* m_refcount;
592 };
593 }
594 #endif // __SGIO_H__

SHOGUN Machine Learning Toolbox - Documentation