aboutsummaryrefslogtreecommitdiffstats
path: root/sualibrary/sua/sua_logging.cpp
blob: 8bc97572e6bff867e7914015efcdaeedc7ba1d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/***************************************************************************
                          sua_logging.cpp  -  description
                             -------------------
    begin                : Tue Jan 8 2002
    copyright            : (C) 2002 by Lode Coene
    email                : lode.coene@siemens.atea.be
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/*
 *  $Id: sua_logging.cpp,v 1.1.1.1 2002/02/04 14:30:41 p82609 Exp $
 *
 * SUA implementation according to SUA draft issue 6.
 *
 * Author(s): Lode Coene
 *            
 *
 * Copyright (C) 2001 by Siemens Atea, Herentals, Belgium.
 *
 * Realized in co-operation between Siemens Atea and 
 * Siemens AG, Munich, Germany.
 *
 * This program 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 2
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact: gery.verwimp@siemens.atea.be
 *          lode.coene@siemens.atea.be
 *
 * The alternative comment
 * inspiration : Cindy
 * "KISS (ME * ) methodology applies"
 * " * The extended KISS method only applies to beautifull girls."
 *
 * Purpose: This code-file defines the SUA logging(error or normal stuff)
 *          functions for:
 *          - initialise the logging file
 *          - log events to logging file
 *          - log byte arrya to logging file
 *          - Display byte array to screen
 *          - close logging file     
 */


#include "sua_debug.h"
#include "sua.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream.h>

#include <sys/time.h>
#include <time.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif


#define  MAX_BYTES_IN_LINE  20

ofstream logfile;
char log_filename[100];

typedef enum { min = -127,
	       max = 128
} sua_byte_str;

/***********************************************************************/
/* init_logging_file                                                   */
/***********************************************************************/
void init_logging_file()
{
  struct timeval tv;
  struct timezone tz;
  struct tm *the_time;
  // random temporary logfile
  //sprintf(log_filename, "./tmp%d.log",(int)getpid());
  sprintf(log_filename, "./tmp2603.log");
  cout << "Logging all errors and events to file " << log_filename <<"\n";
  // creation of random logfile: overwrite file if already exists
  logfile.open(log_filename);
  if (!logfile)
    {
      cout << "Logging file " << log_filename << " could not be created\n";
      exit(0);
    }
  gettimeofday(&tv, &tz);
  the_time = localtime((time_t *) & (tv.tv_sec));
  logfile << the_time->tm_hour << ":" << the_time->tm_min << ":" << the_time->tm_sec << ":" << (int) (tv.tv_usec / 1000) <<  " >> Logfile " << log_filename << " created \n" << flush;

}



/* This function logs events.
   Parameters:
   @param event_log_level : INTERNAL_EVENT_0 INTERNAL_EVENT_1 EXTERNAL_EVENT_X EXTERNAL_EVENT
   @param module_name :     the name of the module that received the event.
   @param log_info :        the info that is printed with the modulename.
   @param anyno :           optional pointer to unsigned int, which is printed along with log_info.
                            The conversion specification must be contained in log_info.
   @author     Hölzlwimmer
*/

/***********************************************************************/
/* event_log                                                           */
/***********************************************************************/
void event_log( char *module_name, char *log_info, ...)
{

#ifdef FILE_DEBUG
  struct timeval tv;
  struct timezone tz;
  struct tm *the_time;

  gettimeofday(&tv, &tz);
  the_time = localtime((time_t *) & (tv.tv_sec));
  logfile << the_time->tm_hour << ":" << the_time->tm_min << ":" << the_time->tm_sec << ":" << (int) (tv.tv_usec / 1000) << " >> " << module_name << " >> " << log_info << " \n" << flush;        
#endif

  return;
}

/***********************************************************************/
/* event_log_byte_array                                                */
/***********************************************************************/
void log_byte_array( char *module_name, char *log_info, int len)
{
#ifdef FILE_DEBUG
  int i,space;
  int li;
  int lines;
  int index;
  struct timeval tv;
  struct timezone tz;
  struct tm *the_time;


  gettimeofday(&tv, &tz);
  the_time = localtime((time_t *) & (tv.tv_sec));
  logfile << the_time->tm_hour << ":" << the_time->tm_min << ":" << the_time->tm_sec << ":" << (int) (tv.tv_usec / 1000) << " >> " << module_name << " >> " ;
  logfile << "Display byte array(in Hex), length " << len << "(hex " << hex << len << ")\n";
 
  lines = (len / MAX_BYTES_IN_LINE) + 1;
  li = 0;
  index = 0;
  logfile.setf(ios::hex);
  while ( li < lines){
    i = 0;
    space = 0;
    while (( i < MAX_BYTES_IN_LINE) && (index < len)) {
      /* what is needed to display a single byte in hex */
      /* convert the char element to a unsigned character : weird??? */
      /* then convert the unsigned char to a unsigned short */
      /* do the display of the unsigned short */
      unsigned char usc = (log_info[index]);
      unsigned short c = usc;
      logfile << hex << setfill('0') << setw(2) << c;
      space++;
      if (space >= 4) {
	logfile << " ";
	space = 0;
      }
      index++;
      i++;
    }
    logfile << "\n";
    li++;
  }
  logfile.setf(ios::dec);
  logfile << flush;
#endif
      
  return;
}


/***********************************************************************/
/* display_byte_array                                                  */
/***********************************************************************/
void display_byte_array( char *log_info, int len)
{
  int i,li, lines,index, space;
    
  lines = (len / MAX_BYTES_IN_LINE) + 1;
  li = 0;
  index = 0;
 

  //cout << "lines = " << lines  << " , len = " << len << "\n";
  cout.setf(ios::hex);
  while ( li < lines){
    i = 0;
    space = 0;
    while (( i < MAX_BYTES_IN_LINE) && (index < len)) {
      /* what is needed to display a single byte in hex */
      /* convert the char element to a unsigned character : weird??? */
      /* then convert the unsigned char to a unsigned short */
      /* do the display of the unsigned short */
      unsigned char usc = (log_info[index]);
      unsigned short c = usc;
      cout << hex << setfill('0') << setw(2) << c;
      space++;
      if (space >= 4) {
	cout << " ";
	space = 0;
      }
      index++;
      i++;
    }
    cout << "\n";
    // cout << dec << "i = " << i << " , li = " << li << " , index = " << index << "\n";
    li++;
  }
  cout.setf(ios::dec);
  cout << "SUA message size = " << len << "\n";

}


/***********************************************************************/
/* close_logging_file                                                  */
/***********************************************************************/
void close_logging_file()
{
  struct timeval tv;
  struct timezone tz;
  struct tm *the_time;
 
  gettimeofday(&tv, &tz);
  the_time = localtime((time_t *) & (tv.tv_sec));
  logfile << the_time->tm_hour << ":" << the_time->tm_min << ":" << the_time->tm_sec << ":" << (int) (tv.tv_usec / 1000) << " >> Logfile " << log_filename << " closed \n" << flush;
  
  // closing of random logfile
  logfile.close();
  
}