Goose Management Model ODdox  1.02
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 <iostream>
37 #include <fstream>
38 #include <string.h>
39 #include "maperrormsg.h"
40 
42 
43 MapErrorMsg::MapErrorMsg( string a_warnfile )
44 {
45  m_warnfile = a_warnfile;
46 }
47 
49 {
50  if ( a_level > WARN_BUG && a_level <= WARN_ALL )
51  m_level = a_level;
52  else {
53  m_level = WARN_ALL;
54  Warn( WARN_BUG, "MapErrorMsg::SetWarnLevel(): Illegal error level!", "");
55  exit(1);
56  }
57 }
58 
60  std::string a_msg1,
61  std::string a_msg2)
62 {
63  FILE * EFile;
64  time_t aclock;
65  tm* newtime;
66  time(&aclock); // Get time in seconds.
67  newtime = localtime(&aclock); // Convert time to struct tm form.
68 
69  // Print local time as a string
70  EFile = fopen(m_warnfile.c_str(), "a+");
71  if (!EFile) {
72  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
73  " for error messages: %s\n", m_warnfile.c_str());
74  exit(1);
75  }
76 
77  fprintf(EFile, "%s Level %d *****\n%s %s\n",
78  asctime(newtime), a_level, a_msg1.c_str(), a_msg2.c_str());
79  fflush(EFile);
80  fclose(EFile);
81 }
82 
83 void MapErrorMsg::Warn(MapErrorState a_level, std::string a_msg1, int a_msg2)
84 {
85  FILE * EFile;
86  time_t aclock;
87  tm* newtime;
88  time(&aclock); // Get time in seconds.
89  newtime = localtime(&aclock); // Convert time to struct tm form.
90 
91  // Print local time as a string
92  EFile = fopen(m_warnfile.c_str(), "a+");
93  if (!EFile) {
94  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
95  " for error messages: %s\n", m_warnfile.c_str());
96  exit(1);
97  }
98 
99  fprintf(EFile, "%s Level %d *****\n%s %d\n",
100  asctime(newtime), a_level, a_msg1.c_str(), a_msg2);
101  fflush(EFile);
102  fclose(EFile);
103 }
104 
105 void MapErrorMsg::Warn(std::string a_msg1, std::string a_msg2)
106 {
107  FILE * EFile;
108  time_t aclock;
109  tm* newtime;
110  time( &aclock ); // Get time in seconds.
111  newtime=localtime( &aclock ); // Convert time to struct tm form.
112  // Print local time as a string.
113 
114  EFile=fopen(m_warnfile.c_str(), "a+" );
115  if ( !EFile ) {
116  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
117  " for error messages: %s\n", m_warnfile.c_str() );
118  exit(1);
119  }
120 
121  fprintf( EFile, "%s *****\n%s %s\n",
122  asctime(newtime), a_msg1.c_str(), a_msg2.c_str() );
123  fflush( EFile );
124  fclose( EFile );
125 }
126 
127 void MapErrorMsg::Warn( std::string a_msg1, double a_num )
128 {
129  FILE * EFile;
130  time_t aclock;
131  tm* newtime;
132  time( &aclock ); // Get time in seconds.
133  newtime=localtime( &aclock ); // Convert time to struct tm form.
134  // Print local time as a string.
135 
136  EFile=fopen(m_warnfile.c_str(), "a+" );
137  if ( !EFile ) {
138  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
139  " for error messages: %s\n", m_warnfile.c_str() );
140  exit(1);
141  }
142 
143  fprintf( EFile, "%s *****\n%s %g\n",
144  asctime(newtime), a_msg1.c_str(), a_num );
145  fflush( EFile );
146  fclose( EFile );
147 }
148 
149 void MapErrorMsg::WarnAddInfo(MapErrorState a_level, std::string a_add1, std::string a_add2)
150 {
151  FILE * EFile;
152  if (a_level > m_level) return;
153  EFile = fopen(m_warnfile.c_str(), "a+");
154  if (!EFile) {
155  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
156  " for error messages: %s\n", m_warnfile.c_str());
157  exit(1);
158  }
159  fprintf(EFile, "%s%s", a_add1.c_str(), a_add2.c_str());
160  fflush(EFile);
161  fclose(EFile);
162 }
163 
164 void MapErrorMsg::WarnAddInfo(MapErrorState a_level, std::string a_add1, double a_num)
165 {
166  if (a_level > m_level) return;
167  ofstream *EFile;
168  EFile = new ofstream(m_warnfile.c_str(), ios::app);
169  if (!EFile->is_open()) {
170  fprintf(stderr, "MapErrorMsg::Warn(): Unable to open file"
171  " for error messages: %s\n", m_warnfile.c_str());
172  exit(1);
173  }
174  (*EFile) << a_add1.c_str() << a_num << endl;
175  EFile->close();
176 }
177 
178 #ifdef __UNIX__
179 int random( int a_range )
180 {
181  // Want to raise exception on this?
182  if ( a_range <= 0 )
183  return 0;
184 
185  return (int)(rand()%a_range);
186  //long inter = rand()*(long)a_range;
187  //return (int)(inter/(long)RAND_MAX);
188 }
189 #endif // __UNIX__
190 
191 
MapErrorMsg::WarnAddInfo
void WarnAddInfo(MapErrorState a_level, std::string a_add1, std::string a_add2)
Definition: maperrormsg.cpp:149
WARN_BUG
Definition: maperrormsg.h:34
MapErrorMsg::m_level
MapErrorState m_level
Definition: maperrormsg.h:45
g_msg
class MapErrorMsg * g_msg
This pointer provides access the to the internal ALMaSS error message system.
Definition: maperrormsg.cpp:41
WARN_ALL
Definition: maperrormsg.h:40
MapErrorMsg
Definition: maperrormsg.h:43
MapErrorMsg::SetWarnLevel
void SetWarnLevel(MapErrorState a_level)
Definition: maperrormsg.cpp:48
maperrormsg.h
MapErrorMsg::Warn
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:59
MapErrorState
MapErrorState
Definition: maperrormsg.h:33
MapErrorMsg::m_warnfile
std::string m_warnfile
Definition: maperrormsg.h:46
MapErrorMsg::MapErrorMsg
MapErrorMsg(std::string a_warnfile)
Definition: maperrormsg.cpp:43