SHOGUN  6.1.3
SGIO.cpp
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-2009 Soeren Sonnenburg
8  * Written (W) 1999-2009 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/io/SGIO.h>
14 #include <shogun/lib/common.h>
15 #include <shogun/base/init.h>
16 #include <shogun/lib/memory.h>
17 #include <shogun/lib/Time.h>
19 #include <shogun/lib/RefCount.h>
20 
21 #include <stdarg.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <sys/stat.h>
25 #ifdef _WIN32
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include <stdlib.h>
31 
32 #ifdef _WIN32
33 #define R_OK 4
34 #endif
35 
36 using namespace shogun;
37 
40 
41 const char* SGIO::message_strings[NUM_LOG_LEVELS]={"[GCDEBUG] \0", "[DEBUG] \0", "[INFO] \0",
42  "[NOTICE] \0", "[WARN] \0", "[ERROR] \0",
43  "[CRITICAL] \0", "[ALERT] \0", "[EMERGENCY] \0", "\0"};
44 
45 const char* SGIO::message_strings_highlighted[NUM_LOG_LEVELS]={"[GCDEBUG] \0", "[DEBUG] \0", "[INFO] \0",
46  "[NOTICE] \0", "\033[1;34m[WARN]\033[0m \0", "\033[1;31m[ERROR]\033[0m \0",
47  "[CRITICAL] \0", "[ALERT] \0", "[EMERGENCY] \0", "\0"};
48 
51 
54 
56  : target(stdout), show_progress(false), location_info(MSG_NONE),
57  syntax_highlight(true), loglevel(MSG_WARN)
58 {
59  m_refcount = new RefCount();
60 }
61 
62 SGIO::SGIO(const SGIO& orig)
66  loglevel(orig.get_loglevel())
67 {
68  m_refcount = new RefCount();
69 }
70 
71 void SGIO::message(EMessageType prio, const char* function, const char* file,
72  int32_t line, const char *fmt, ... ) const
73 {
74  const char* msg_intro=get_msg_intro(prio);
75 
76  if (msg_intro)
77  {
78  char str[4096];
79  snprintf(str, sizeof(str), "%s", msg_intro);
80  int len=strlen(msg_intro);
81  char* s=str+len;
82 
83  /* file and line are shown for warnings and worse */
84  if (location_info==MSG_LINE_AND_FILE || prio==MSG_WARN || prio==MSG_ERROR)
85  {
86  snprintf(s, sizeof(str)-len, "In file %s line %d: ", file, line);
87  len=strlen(str);
88  s=str+len;
89  }
90  else if (location_info==MSG_FUNCTION)
91  {
92  snprintf(s, sizeof(str)-len, "%s: ", function);
93  len=strlen(str);
94  s=str+len;
95  }
96  else if (location_info==MSG_NONE)
97  {
98  ;
99  }
100 
101  va_list list;
102  va_start(list,fmt);
103  vsnprintf(s, sizeof(str)-len, fmt, list);
104  va_end(list);
105 
106  switch (prio)
107  {
108  case MSG_GCDEBUG:
109  case MSG_DEBUG:
110  case MSG_INFO:
111  case MSG_NOTICE:
112  case MSG_MESSAGEONLY:
113  if (sg_print_message)
114  sg_print_message(target, str);
115  break;
116 
117  case MSG_WARN:
118  if (sg_print_warning)
119  sg_print_warning(target, str);
120  break;
121 
122  case MSG_ERROR:
123  case MSG_CRITICAL:
124  case MSG_ALERT:
125  case MSG_EMERGENCY:
126  if (sg_print_error)
127  sg_print_error(target, str);
128  throw ShogunException(str);
129  break;
130  default:
131  break;
132  }
133 
134  fflush(target);
135  }
136 }
137 
138 void SGIO::buffered_message(EMessageType prio, const char *fmt, ... ) const
139 {
140  const char* msg_intro=get_msg_intro(prio);
141 
142  if (msg_intro)
143  {
144  fprintf(target, "%s", msg_intro);
145  va_list list;
146  va_start(list,fmt);
147  vfprintf(target,fmt,list);
148  va_end(list);
149  }
150 }
151 
152 
154 {
155  if (!show_progress)
156  return;
157 
158  message(MSG_INFO, "", "", -1, "done.\n");
159 }
160 
161 char* SGIO::skip_spaces(char* str)
162 {
163  int32_t i=0;
164 
165  if (str)
166  {
167  for (i=0; isspace(str[i]); i++);
168 
169  return &str[i];
170  }
171  else
172  return str;
173 }
174 
175 char* SGIO::skip_blanks(char* str)
176 {
177  int32_t i=0;
178 
179  if (str)
180  {
181  for (i=0; isblank(str[i]); i++);
182 
183  return &str[i];
184  }
185  else
186  return str;
187 }
188 
190 {
191  return loglevel;
192 }
193 
195 {
196  loglevel=level;
197 }
198 
199 void SGIO::set_target(FILE* t)
200 {
201  target=t;
202 }
203 
204 const char* SGIO::get_msg_intro(EMessageType prio) const
205 {
206  for (int32_t i=NUM_LOG_LEVELS-1; i>=0; i--)
207  {
208  // ignore msg if prio's level is under loglevel,
209  // but not if prio's level higher than MSG_WARN
210  if (levels[i]<loglevel && prio<=MSG_WARN)
211  return NULL;
212 
213  if (levels[i]==prio)
214  {
215  if (syntax_highlight)
216  return message_strings_highlighted[i];
217  else
218  return message_strings[i];
219  }
220  }
221 
222  return NULL;
223 }
224 
226 {
227  uint32_t len = s.end - s.start+1;
228  char* ret = SG_CALLOC(char, len);
229  sg_memcpy(ret,s.start,len-1);
230  return ret;
231 }
232 
234 {
235  char* c_string = c_string_of_substring(s);
236  SG_SPRINT("%s\n", c_string)
237  SG_FREE(c_string);
238 }
239 
241 {
242  char* endptr = s.end;
243  float32_t f = strtof(s.start,&endptr);
244  if (endptr == s.start && s.start != s.end)
245  SG_SERROR("error: %s is not a float!\n", c_string_of_substring(s))
246 
247  return f;
248 }
249 
251 {
252  char* endptr = s.end;
253  float64_t f = strtod(s.start,&endptr);
254  if (endptr == s.start && s.start != s.end)
255  SG_SERROR("Error!:%s is not a double!\n", c_string_of_substring(s))
256 
257  return f;
258 }
259 
261 {
262  char* c_string = c_string_of_substring(s);
263  int32_t int_val = atoi(c_string);
264  SG_FREE(c_string);
265 
266  return int_val;
267 }
268 
270 {
271  return strtoul(s.start,NULL,10);
272 }
273 
275 {
276  return (s.end - s.start);
277 }
278 
279 char* SGIO::concat_filename(const char* filename)
280 {
281 #ifdef WIN32
282  if (snprintf(file_buffer, FBUFSIZE, "%s\\%s", directory_name, filename) > FBUFSIZE)
283 #else
284  if (snprintf(file_buffer, FBUFSIZE, "%s/%s", directory_name, filename) > FBUFSIZE)
285 #endif
286  SG_SERROR("filename too long")
287 
288  SG_SDEBUG("filename=\"%s\"\n", file_buffer)
289  return file_buffer;
290 }
291 
293 {
294  if (d)
295  {
296  char* fname=concat_filename(d->d_name);
297 
298  if (!access(fname, R_OK))
299  {
300  struct stat s;
301  if (!stat(fname, &s) && S_ISREG(s.st_mode))
302  return 1;
303  }
304  }
305 
306  return 0;
307 }
308 
310 {
311  delete m_refcount;
312 }
313 
314 int32_t SGIO::ref()
315 {
316  return m_refcount->ref();
317 }
318 
319 int32_t SGIO::ref_count() const
320 {
321  return m_refcount->ref_count();
322 }
323 
324 int32_t SGIO::unref()
325 {
326  int32_t rc = m_refcount->unref();
327  if (rc==0)
328  {
329  delete this;
330  return 0;
331  }
332 
333  return rc;
334 }
int32_t unref()
Definition: SGIO.cpp:324
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:194
virtual ~SGIO()
Definition: SGIO.cpp:309
std::function< void(FILE *, const char *)> sg_print_message(nullptr)
function called to print normal messages
Definition: init.cpp:52
static char * skip_spaces(char *str)
Definition: SGIO.cpp:161
FILE * get_target() const
Definition: SGIO.h:340
int32_t ref_count() const
Definition: SGIO.cpp:319
static uint32_t ss_length(substring s)
Definition: SGIO.cpp:274
void buffered_message(EMessageType prio, const char *fmt,...) const
Definition: SGIO.cpp:138
char * end
Definition: SGIO.h:209
#define NUM_LOG_LEVELS
Definition: SGIO.h:67
void set_target(FILE *target)
Definition: SGIO.cpp:199
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
char * start
Definition: SGIO.h:207
std::function< void(FILE *, const char *)> sg_print_warning(nullptr)
function called to print warning messages
Definition: init.cpp:55
SG_FORCED_INLINE int32_t ref()
Definition: RefCount.h:26
static char * skip_blanks(char *str)
Definition: SGIO.cpp:175
static int filter(CONST_DIRENT_T *d)
Definition: SGIO.cpp:292
struct Substring, specified by start position and end position.
Definition: SGIO.h:204
std::function< void(FILE *, const char *)> sg_print_error(nullptr)
function called to print error messages
Definition: init.cpp:58
static char file_buffer[FBUFSIZE]
file name buffer
Definition: SGIO.h:537
#define SG_SPRINT(...)
Definition: SGIO.h:165
#define FBUFSIZE
Definition: SGIO.h:68
void message(EMessageType prio, const char *function, const char *file, int32_t line, const char *fmt,...) const
Definition: SGIO.cpp:71
FILE * target
Definition: SGIO.h:518
SG_FORCED_INLINE int32_t unref()
Definition: RefCount.h:35
static char * c_string_of_substring(substring s)
Definition: SGIO.cpp:225
double float64_t
Definition: common.h:60
int32_t ref()
Definition: SGIO.cpp:314
static float32_t float_of_substring(substring s)
Definition: SGIO.cpp:240
static uint32_t ulong_of_substring(substring s)
Definition: SGIO.cpp:269
static const char * message_strings[NUM_LOG_LEVELS]
Definition: SGIO.h:534
static void print_substring(substring s)
Definition: SGIO.cpp:233
EMessageType get_loglevel() const
Definition: SGIO.cpp:189
#define CONST_DIRENT_T
Definition: SGIO.h:81
float float32_t
Definition: common.h:59
static int32_t int_of_substring(substring s)
Definition: SGIO.cpp:260
EMessageType
Definition: SGIO.h:43
bool get_show_progress() const
Definition: SGIO.h:255
EMessageLocation location_info
Definition: SGIO.h:523
EMessageType loglevel
Definition: SGIO.h:528
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SDEBUG(...)
Definition: SGIO.h:153
const char * get_msg_intro(EMessageType prio) const
Definition: SGIO.cpp:204
#define SG_SERROR(...)
Definition: SGIO.h:164
static char directory_name[FBUFSIZE]
directory name buffer
Definition: SGIO.h:539
SG_FORCED_INLINE int32_t ref_count()
Definition: RefCount.h:44
static char * concat_filename(const char *filename)
Definition: SGIO.cpp:279
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
static float64_t double_of_substring(substring s)
Definition: SGIO.cpp:250
static const EMessageType levels[NUM_LOG_LEVELS]
Definition: SGIO.h:530
bool get_syntax_highlight() const
Definition: SGIO.h:271
static const char * message_strings_highlighted[NUM_LOG_LEVELS]
Definition: SGIO.h:532
void done()
Definition: SGIO.cpp:153
EMessageLocation get_location_info() const
Definition: SGIO.h:262

SHOGUN Machine Learning Toolbox - Documentation