hxt_message.c 5.88 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
/*
 * Marblesbag - Copyright (C) <2010-2018>
3
4
 * <Universite catholique de Louvain (UCL), Belgium>
 * <Universite de Montpellier, France>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * 	
 * List of the contributors to the development of SLIM: see AUTHORS file.
 * Description and complete License: see LICENSE file.
 * 	
 * This program (Marblesbag) is free software: 
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not, 
 * see <http://www.gnu.org/licenses/>.
 */

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdlib.h>
#include <stdio.h>
#include "hxt_message.h"
#include <stdarg.h>

#ifdef _OPENMP
  #include <omp.h>
#else
  #define omp_get_thread_num() 0
  #define omp_get_num_threads() 1
#endif

typedef struct {
  /* the message content */
  const char* string; // lifetime = time of callback function

  /* information about the location of the code which sent the message */
  const char* func;   // lifetime = forever
  const char* file;   // lifetime = forever
  const char* line;   // lifetime = forever
  int threadId;      // the thread which sent the message
  int numThreads;    // the number of threads

  enum{
    HXT_MSGLEVEL_INFO     = 0,
    HXT_MSGLEVEL_DEBUG    = 1,
    HXT_MSGLEVEL_WARNING  = 2,
    HXT_MSGLEVEL_ERROR    = 3,
    HXT_MSGLEVEL_TRACE    = 4
  } level;
} HXTMessage;

HXTStatus defaultMessageCallback(HXTMessage* msg);

static HXTStatus (*msgCallback)(HXTMessage* msg) = &defaultMessageCallback; // the message callback is declared as a global variable
static const char* encodingIssue = "~~~~ (encoding issue) ~~~~";


const char*  hxtGetStatusString(HXTStatus status){
  switch(status){
    case HXT_STATUS_OK                   :
      return "no error";
      break;
    case HXT_STATUS_FAILED               :
      return "function failed";
      break;
    case HXT_STATUS_OUT_OF_MEMORY        :
      return "out of memory";
      break;
    case HXT_STATUS_ERROR                :
      return "error";
      break;
    case HXT_STATUS_FILE_CANNOT_BE_OPENED:
      return "file cannot be opened";
      break;
    case HXT_STATUS_ASSERTION_FAILED:
      return "assertion failed";
      break;
    case HXT_STATUS_POINTER_ERROR:
      return "wrong pointer given";
      break;
    default:
      if(status<=HXT_STATUS_INTERNAL)
        return "internal error was not catched. This should not happen";
      else if(status<0)
        return "unknow error";
      else
        return "positive return value";
      break;
  }
}



HXTStatus defaultMessageCallback(HXTMessage* msg){
  if (msg->level == HXT_MSGLEVEL_INFO)
    fprintf(stdout,"Info : %s\n", msg->string);
  else if (msg->level == HXT_MSGLEVEL_ERROR)
    fprintf(stderr,"= X = Error : %s   \n in %s -> %s:%s\n", msg->string, msg->func, msg->file, msg->line);
  else if (msg->level == HXT_MSGLEVEL_TRACE)
    fprintf(stderr,"  - trace -   %s -> %s:%s  \t(%s)\n", msg->func, msg->file, msg->line, msg->string);
  else if (msg->level == HXT_MSGLEVEL_WARNING)
    fprintf(stderr,"/!\\ Warning : %s\n", msg->string);
  else if (msg->level == HXT_MSGLEVEL_DEBUG)
    fprintf(stderr,"Debug : %s   \t(in %s -> %s:%s)\n", msg->string, msg->func, msg->file, msg->line);
  return HXT_STATUS_OK;
}


HXTStatus  hxtSetMessageCallback(HXTStatus (*newMsgCallback)(HXTMessage* msg)){
  if(newMsgCallback==NULL)
    msgCallback = defaultMessageCallback;
  else
    msgCallback = newMsgCallback;
  return HXT_STATUS_OK;
}


static void hxtMessageGeneral ( int messageLevel,
                                  const char* func,
                                  const char* file,
                                  const char* line,
                                  const char* encodingIssueString,
                                  const char* alternativeString,
                                  const char *fmt, va_list args){

  HXTMessage msg;

  char str[5000];
  if(fmt!=NULL){
    int err = vsnprintf(str, sizeof(str), fmt, args);

    if(err>=0)
      msg.string = str;
    else
      msg.string = encodingIssueString;
  }
  else{
    msg.string = alternativeString;
  }

  msg.func = func;
  msg.file = file;
  msg.line = line;
  msg.level = messageLevel;

  msg.threadId = omp_get_thread_num();
  msg.numThreads = omp_get_num_threads();

  // #pragma omp critical
  {
    msgCallback(&msg);
  }
}


HXTStatus  hxtMessageInfo        ( const char* func, const char* file, const char* line, const char *fmt, ...){
  va_list args;
  va_start(args, fmt);
  hxtMessageGeneral(HXT_MSGLEVEL_INFO, func, file, line, encodingIssue, "", fmt, args);
  va_end(args);
  return HXT_STATUS_OK;
}


HXTStatus  hxtMessageWarning        ( const char* func, const char* file, const char* line, const char *fmt, ...){
  va_list args;
  va_start(args, fmt);
  hxtMessageGeneral(HXT_MSGLEVEL_WARNING, func, file, line, encodingIssue, "", fmt, args);
  return HXT_STATUS_OK;
}


HXTStatus  hxtMessageError       ( HXTStatus status, const char* func, const char* file, const char* line, const char *fmt, ...){
  va_list args;
  va_start(args, fmt);
  hxtMessageGeneral(HXT_MSGLEVEL_ERROR, func, file, line, hxtGetStatusString(status), hxtGetStatusString(status), fmt, args);
  va_end(args);
  return status;
}


HXTStatus  hxtMessageTraceError( HXTStatus status, const char* func, const char* file, const char* line, const char *fmt, ...){
  if(status==HXT_STATUS_OK)
    return HXT_STATUS_OK;
  va_list args;
  va_start(args, fmt);
  hxtMessageGeneral(HXT_MSGLEVEL_TRACE, func, file, line, encodingIssue, hxtGetStatusString(status), fmt, args);
  va_end(args);
  return status;
}