ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
maperrormsg.cpp
Go to the documentation of this file.
1 //
2 // maperrormsg.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, University of Aarhus
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification, are permitted provided
10 that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions and the
13 following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
15 the following disclaimer in the documentation and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
20 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ********************************************************************************************************
26 */
27 
28 #define _CRT_SECURE_NO_DEPRECATE
29 
30 using namespace std;
31 
32 #include <cstdio>
33 #include <time.h>
34 #include <string>
35 #include <cstdlib>
36 #include "maperrormsg.h"
37 
39 
40 MapErrorMsg::MapErrorMsg( string a_warnfile )
41 {
42  m_warnfile = a_warnfile;
43 }
44 
46 {
47  if ( a_level > WARN_BUG && a_level <= WARN_ALL )
48  m_level = a_level;
49  else {
50  m_level = WARN_ALL;
51  Warn( WARN_BUG, "MapErrorMsg::SetWarnLevel(): Illegal error level!", "");
52  exit(1);
53  }
54 }
55 
57  std::string a_msg1,
58  std::string a_msg2)
59 {
60  FILE * EFile;
61  time_t aclock;
62  tm* newtime;
63  time(&aclock); // Get time in seconds.
64  newtime = localtime(&aclock); // Convert time to struct tm form.
65 
66  // Print local time as a string
67  EFile = fopen(m_warnfile.c_str(), "a+");
68  if (!EFile) {
69  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
70  " for error messages: %s\n", m_warnfile.c_str());
71  exit(1);
72  }
73 
74  fprintf(EFile, "%s Level %d *****\n%s %s\n",
75  asctime(newtime), a_level, a_msg1.c_str(), a_msg2.c_str());
76  fflush(EFile);
77  fclose(EFile);
78 }
79 
80 void MapErrorMsg::Warn(MapErrorState a_level, std::string a_msg1, int a_msg2)
81 {
82  FILE * EFile;
83  time_t aclock;
84  tm* newtime;
85  time(&aclock); // Get time in seconds.
86  newtime = localtime(&aclock); // Convert time to struct tm form.
87 
88  // Print local time as a string
89  EFile = fopen(m_warnfile.c_str(), "a+");
90  if (!EFile) {
91  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
92  " for error messages: %s\n", m_warnfile.c_str());
93  exit(1);
94  }
95 
96  fprintf(EFile, "%s Level %d *****\n%s %d\n",
97  asctime(newtime), a_level, a_msg1.c_str(), a_msg2);
98  fflush(EFile);
99  fclose(EFile);
100 }
101 
102 void MapErrorMsg::Warn(std::string a_msg1, std::string a_msg2)
103 {
104  FILE * EFile;
105  time_t aclock;
106  tm* newtime;
107  time( &aclock ); // Get time in seconds.
108  newtime=localtime( &aclock ); // Convert time to struct tm form.
109  // Print local time as a string.
110 
111  EFile=fopen(m_warnfile.c_str(), "a+" );
112  if ( !EFile ) {
113  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
114  " for error messages: %s\n", m_warnfile.c_str() );
115  exit(1);
116  }
117 
118  fprintf( EFile, "%s *****\n%s %s\n",
119  asctime(newtime), a_msg1.c_str(), a_msg2.c_str() );
120  fflush( EFile );
121  fclose( EFile );
122 }
123 
124 void MapErrorMsg::Warn( std::string a_msg1, double a_num )
125 {
126  FILE * EFile;
127  time_t aclock;
128  tm* newtime;
129  time( &aclock ); // Get time in seconds.
130  newtime=localtime( &aclock ); // Convert time to struct tm form.
131  // Print local time as a string.
132 
133  EFile=fopen(m_warnfile.c_str(), "a+" );
134  if ( !EFile ) {
135  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
136  " for error messages: %s\n", m_warnfile.c_str() );
137  exit(1);
138  }
139 
140  fprintf( EFile, "%s *****\n%s %g\n",
141  asctime(newtime), a_msg1.c_str(), a_num );
142  fflush( EFile );
143  fclose( EFile );
144 }
145 
147  std::string a_add1,
148  std::string a_add2 )
149 {
150  FILE * EFile;
151 
152  if ( a_level > m_level )
153  return;
154 
155  EFile=fopen(m_warnfile.c_str(), "a+" );
156  if (!EFile) {
157  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
158  " for error messages: %s\n", m_warnfile.c_str() );
159  exit(1);
160  }
161 
162  fprintf( EFile, "%s%s", a_add1.c_str(), a_add2.c_str() );
163  fflush( EFile );
164  fclose( EFile );
165 }
166 
167 #ifdef __UNIX__
168 int random( int a_range )
169 {
170  // Want to raise exception on this?
171  if ( a_range <= 0 )
172  return 0;
173 
174  return (int)(rand()%a_range);
175  //long inter = rand()*(long)a_range;
176  //return (int)(inter/(long)RAND_MAX);
177 }
178 #endif // __UNIX__
179 
180 
MapErrorState m_level
Definition: maperrormsg.h:45
void WarnAddInfo(MapErrorState a_level, std::string a_add1, std::string a_add2)
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
void SetWarnLevel(MapErrorState a_level)
Definition: maperrormsg.cpp:45
MapErrorMsg(std::string a_warnfile)
Definition: maperrormsg.cpp:40
MapErrorState
Definition: maperrormsg.h:33
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
std::string m_warnfile
Definition: maperrormsg.h:46