SHOGUN  3.2.2
 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 <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 funktions (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__

SHOGUN Machine Learning Toolbox - Documentation