File: Logging.cpp

package info (click to toggle)
csmash 0.6.4.2-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,704 kB
  • ctags: 1,954
  • sloc: cpp: 17,031; ansic: 4,391; sh: 2,956; makefile: 615; yacc: 318
file content (263 lines) | stat: -rw-r--r-- 7,163 bytes parent folder | download
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
/* $Id: Logging.cpp,v 1.9 2002/02/14 16:53:15 nan Exp $ */

// Copyright (C) 2001, 2002   ศน(Kanna Yoshihiro)
//
// 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

#include "ttinc.h"
#ifdef LOGGING
#include "MultiPlay.h"
#include "Event.h"
#include "Ball.h"
#include "Player.h"
#include "Logging.h"

Logging* Logging::m_logging = NULL;

Logging::Logging() {
    memset(m_fp, 0, sizeof(m_fp));
}

Logging::~Logging() {
  for ( int i = 0 ; i < 8 ; i++ ) {
    if (m_fp[i]) fclose( m_fp[i] );
  }
}

Logging*
Logging::GetLogging() {
  if ( !Logging::m_logging )
    Logging::m_logging = new Logging();

  return Logging::m_logging;
}

bool
Logging::Init() {
  static const char* fname[] = {
      "log/com_ball.log",
      "log/com_thePlayer.log", 
      "log/com_comPlayer.log",
      "log/com_misc.log", 
      "log/act_ball.log",
      "log/act_thePlayer.log", 
      "log/act_comPlayer.log",
      "log/act_misc.log",
  };

  for ( int i = 0 ; i < 8; i++ ) {
    if ( (m_fp[i] = fopen( fname[i], "w" )) == 0 )
      return false;
  }

  return true;
}

bool
Logging::Log( long logType, char *logString ) {
    if (m_fp[logType]) {
        fputs( logString, m_fp[logType] );
        fflush( m_fp[logType] );
    }
  return true;
}

bool
Logging::StartLog() {
  struct timeb tb;
  char buf[256];

#ifndef WIN32
  struct timeval tv;
  struct timezone tz;
#endif

#ifdef WIN32
  ftime( &tb );
#else
  gettimeofday( &tv, &tz );
  tb.time = tv.tv_sec;
  tb.millitm = tv.tv_usec/1000;
#endif

  snprintf( buf, sizeof(buf), "--- START LOGGING %d.%3d ---\n",
            (int)tb.time, (int)tb.millitm );

  for ( int i = 0 ; i < 7 ; i++ )
    Log( i, buf );

  return true;
}

bool
Logging::LogTime( long logType, struct timeb *tb ) {
  char buf[64];

  snprintf( buf, sizeof(buf), "%d.%3d: ", (int)tb->time, (int)tb->millitm );
  Log( logType, buf );

  return true;
}

bool
Logging::LogTime( long logType ) {
  char buf[64];
  long sec, count;

  sec = Event::m_lastTime.time;
  count = Event::m_lastTime.millitm/10;
  Event::GetAdjustedTime( sec, count );
  snprintf( buf, sizeof(buf), "%d.%2d: ", sec, count );
  Log( logType, buf );

  return true;
}

bool
Logging::LogBall( long logType, Ball *ball ) {
  char buf[1024];

  LogTime( logType );
  snprintf( buf, sizeof(buf),
           "status = %2d x = %4.2f y = %4.2f z = %4.2f "
           "vx = %4.2f vy = %4.2f vz = %4.2f spin = %3.2f\n",
	   (int)ball->GetStatus(), ball->GetX(), ball->GetY(), ball->GetZ(),
           ball->GetVX(), ball->GetVY(), ball->GetVZ(), ball->GetSpin() );
  Log( logType, buf );

  return true;
}

bool
Logging::LogPlayer( long logType, Player *player ) {
  char buf[1024];

  LogTime( logType );
  snprintf( buf, sizeof(buf),
            "playerType=%1d side=%2d x=%4.2f y=%4.2f z=%4.2f "
            "vx=%4.2f vy=%4.2f vz=%4.2f status=%2d "
            "swing=%2d swingType=%1d swingSide=%2d afterSwing=%2d "
            "swingError=%1d targetX=%4.2f targetY=%4.2f "
            "eyeX=%4.2f eyeY=%4.2f eyeZ=%4.2f "
            "lookAtX=%4.2f lookAtY=%4.2f lookAtZ=%4.2f "
            "pow=%1d spin=%3.2f stamina=%2.0f dragX=%2d dragY=%2d\n",
            (int)player->GetPlayerType(), (int)player->GetSide(), 
            player->GetX(), player->GetY(), player->GetZ(), 
            player->GetVX(), player->GetVY(), player->GetVZ(), 
            (int)player->GetStatus(), (int)player->GetSwing(),
            (int)player->GetSwingType(), (int)player->GetSwingSide(),
            (int)player->GetAfterSwing(), (int)player->GetSwingError(), 
            player->GetTargetX(), player->GetTargetY(),
            player->GetEyeX(), player->GetEyeY(), player->GetEyeZ(),
            player->GetLookAtX(), player->GetLookAtY(), player->GetLookAtZ(),
            (int)player->GetPower(), player->GetSpin(), player->GetStamina(),
            (int)player->GetDragX(), (int)player->GetDragY() );
  Log( logType, buf );

  return true;
}

bool
Logging::LogRecvBVMessage( ExternalBVData *bv ) {
  char buf[256];

  LogTime( LOG_COMBALL );
  snprintf( buf, sizeof(buf), "recv: %d.%3d ", (int)bv->sec, (int)bv->count );
  Log( LOG_COMBALL, buf );
  Ball *tmpBall = new Ball();
  tmpBall->Warp(bv->data);

  snprintf( buf, sizeof(buf),
            "x=%4.2f y=%4.2f z=%4.2f "
            "vx=%4.2f vy=%4.2f vz=%4.2f spin=%3.2f status=%2d\n",
	   tmpBall->GetX(), tmpBall->GetY(), tmpBall->GetZ(), 
	   tmpBall->GetVX(), tmpBall->GetVY(), tmpBall->GetVZ(), 
	   tmpBall->GetSpin(), (int)tmpBall->GetStatus() );
  Log( LOG_COMBALL, buf );

  return true;
}

bool
Logging::LogSendPVMessage( Player *player ) {
  char buf[256];

  LogTime( LOG_COMTHEPLAYER );
  snprintf( buf, sizeof(buf),
           "send PV: x=%4.2f y=%4.2f z=%4.2f vx=%4.2f vy=%4.2f vz=%4.2f\n",
	   player->GetX(), player->GetY(), player->GetZ(), 
	   player->GetVX(), player->GetVY(), player->GetVZ() );
  Log( LOG_COMTHEPLAYER, buf );

  return true;
}

bool
Logging::LogRecvPVMessage( ExternalPVData *pv ) {
  char buf[256];

  LogTime( LOG_COMCOMPLAYER );
  snprintf( buf, sizeof(buf),
            "recv PV: %d.%3d ", (int)pv->sec, (int)pv->count );
  Log( LOG_COMCOMPLAYER, buf );
  Player *tmpPlayer = new Player();
  tmpPlayer->Warp(pv->data);

  snprintf( buf, sizeof(buf),
            "x=%4.2f y=%4.2f z=%4.2f vx=%4.2f vy=%4.2f vz=%4.2f\n",
            tmpPlayer->GetX(), tmpPlayer->GetY(), tmpPlayer->GetZ(), 
            tmpPlayer->GetVX(), tmpPlayer->GetVY(), tmpPlayer->GetVZ() );
  Log( LOG_COMCOMPLAYER, buf );

  return true;
}

bool
Logging::LogSendPSMessage( Player *player ) {
  char buf[256];

  LogTime( LOG_COMTHEPLAYER );

  snprintf( buf, sizeof(buf),
            "send PS: pow=%2d spin=%3.2f swingType=%1d swingSide=%2d swing=%2d\n",
	   (int)player->GetPower(), player->GetSpin(),
	   (int)player->GetSwingType(), player->GetSwingSide(),
	   (int)player->GetSwing() );
  Log( LOG_COMTHEPLAYER, buf );

  return true;
}

bool
Logging::LogRecvPSMessage( ExternalPSData *ps ) {
  char buf[256];

  LogTime( LOG_COMCOMPLAYER );
  snprintf( buf, sizeof(buf),
            "recv PS: %d.%3d ", (int)ps->sec, (int)ps->count );
  Log( LOG_COMCOMPLAYER, buf );
  Player *tmpPlayer = new Player();
  tmpPlayer->ExternalSwing(ps->data);

  snprintf( buf, sizeof(buf),
           "pow=%2d spin=%3.2f swingType=%1d swingSide=%2d swing=%2d\n",
	   (int)tmpPlayer->GetPower(), tmpPlayer->GetSpin(),
	   (int)tmpPlayer->GetSwingType(), tmpPlayer->GetSwingSide(),
	   (int)tmpPlayer->GetSwing() );
  Log( LOG_COMCOMPLAYER, buf );

  return true;
}
#endif