1 /* $Id: Logging.cpp,v 1.12 2003/08/17 11:09:23 nan Exp $ */
2 
3 // Copyright (C) 2001, 2002, 2003  ���� �ȹ�(Kanna Yoshihiro)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19 #include "ttinc.h"
20 #ifdef LOGGING
21 #include "MultiPlay.h"
22 #include "Event.h"
23 #include "Ball.h"
24 #include "Player.h"
25 #include "Network.h"
26 #include "Logging.h"
27 
28 Logging* Logging::m_logging = NULL;
29 
Logging()30 Logging::Logging() {
31     memset(m_fp, 0, sizeof(m_fp));
32 }
33 
~Logging()34 Logging::~Logging() {
35   for ( int i = 0 ; i < 8 ; i++ ) {
36     if (m_fp[i]) fclose( m_fp[i] );
37   }
38 }
39 
40 Logging*
GetLogging()41 Logging::GetLogging() {
42   if ( !Logging::m_logging )
43     Logging::m_logging = new Logging();
44 
45   return Logging::m_logging;
46 }
47 
48 bool
Init()49 Logging::Init() {
50   static const char* fname[] = {
51       "log/com_ball.log",
52       "log/com_thePlayer.log",
53       "log/com_comPlayer.log",
54       "log/com_misc.log",
55       "log/act_ball.log",
56       "log/act_thePlayer.log",
57       "log/act_comPlayer.log",
58       "log/act_misc.log",
59   };
60 
61   for ( int i = 0 ; i < 8; i++ ) {
62     if ( (m_fp[i] = fopen( fname[i], "w" )) == 0 )
63       return false;
64   }
65 
66   return true;
67 }
68 
69 bool
Log(long logType,char * logString)70 Logging::Log( long logType, char *logString ) {
71     if (m_fp[logType]) {
72         fputs( logString, m_fp[logType] );
73         fflush( m_fp[logType] );
74     }
75   return true;
76 }
77 
78 bool
StartLog()79 Logging::StartLog() {
80   struct timeb tb;
81   char buf[256];
82 
83 #ifndef WIN32
84   struct timeval tv;
85   struct timezone tz;
86 #endif
87 
88 #ifdef WIN32
89   ftime( &tb );
90 #else
91   gettimeofday( &tv, &tz );
92   tb.time = tv.tv_sec;
93   tb.millitm = tv.tv_usec/1000;
94 #endif
95 
96   snprintf( buf, sizeof(buf), "--- START LOGGING %d.%3d ---\n",
97             (int)tb.time, (int)tb.millitm );
98 
99   for ( int i = 0 ; i < 7 ; i++ )
100     Log( i, buf );
101 
102   return true;
103 }
104 
105 bool
LogTime(long logType,struct timeb * tb)106 Logging::LogTime( long logType, struct timeb *tb ) {
107   char buf[64];
108 
109   snprintf( buf, sizeof(buf), "%d.%3d: ", (int)tb->time, (int)tb->millitm );
110   Log( logType, buf );
111 
112   return true;
113 }
114 
115 bool
LogTime(long logType)116 Logging::LogTime( long logType ) {
117   char buf[64];
118   long sec, count;
119 
120   sec = Event::m_lastTime.time;
121   count = Event::m_lastTime.millitm/10;
122   Event::GetAdjustedTime( sec, count );
123   snprintf( buf, sizeof(buf), "%d.%2d: ", (int)sec, (int)count );
124   Log( logType, buf );
125 
126   return true;
127 }
128 
129 bool
LogBall(long logType,Ball * ball)130 Logging::LogBall( long logType, Ball *ball ) {
131   char buf[1024];
132 
133   LogTime( logType );
134   snprintf( buf, sizeof(buf),
135            "status = %2d x = %4.2f y = %4.2f z = %4.2f "
136            "vx = %4.2f vy = %4.2f vz = %4.2f spin = %3.2f\n",
137 	   (int)ball->GetStatus(), ball->GetX(), ball->GetY(), ball->GetZ(),
138            ball->GetVX(), ball->GetVY(), ball->GetVZ(), ball->GetSpin() );
139   Log( logType, buf );
140 
141   return true;
142 }
143 
144 bool
LogPlayer(long logType,Player * player)145 Logging::LogPlayer( long logType, Player *player ) {
146   char buf[1024];
147 
148   LogTime( logType );
149   snprintf( buf, sizeof(buf),
150             "playerType=%1d side=%2d x=%4.2f y=%4.2f z=%4.2f "
151             "vx=%4.2f vy=%4.2f vz=%4.2f status=%2d "
152             "swing=%2d swingType=%1d swingSide=%2d afterSwing=%2d "
153             "swingError=%1d targetX=%4.2f targetY=%4.2f "
154             "eyeX=%4.2f eyeY=%4.2f eyeZ=%4.2f "
155             "lookAtX=%4.2f lookAtY=%4.2f lookAtZ=%4.2f "
156             "pow=%1d spin=%3.2f stamina=%2.0f dragX=%2d dragY=%2d\n",
157             (int)player->GetPlayerType(), (int)player->GetSide(),
158             player->GetX(), player->GetY(), player->GetZ(),
159             player->GetVX(), player->GetVY(), player->GetVZ(),
160             (int)player->GetStatus(), (int)player->GetSwing(),
161             (int)player->GetSwingType(), (int)player->GetSwingSide(),
162             (int)player->GetAfterSwing(), (int)player->GetSwingError(),
163             player->GetTargetX(), player->GetTargetY(),
164             player->GetEyeX(), player->GetEyeY(), player->GetEyeZ(),
165             player->GetLookAtX(), player->GetLookAtY(), player->GetLookAtZ(),
166             (int)player->GetPower(), player->GetSpin(), player->GetStamina(),
167             (int)player->GetDragX(), (int)player->GetDragY() );
168   Log( logType, buf );
169 
170   return true;
171 }
172 
173 bool
LogRecvBVMessage(ExternalBVData * bv)174 Logging::LogRecvBVMessage( ExternalBVData *bv ) {
175   char buf[256];
176 
177   LogTime( LOG_COMBALL );
178   snprintf( buf, sizeof(buf), "recv: %d.%3d ", (int)bv->sec, (int)bv->count );
179   Log( LOG_COMBALL, buf );
180   Ball *tmpBall = new Ball();
181   tmpBall->Warp(bv->data);
182 
183   snprintf( buf, sizeof(buf),
184             "x=%4.2f y=%4.2f z=%4.2f "
185             "vx=%4.2f vy=%4.2f vz=%4.2f spin=%3.2f status=%2d\n",
186 	   tmpBall->GetX(), tmpBall->GetY(), tmpBall->GetZ(),
187 	   tmpBall->GetVX(), tmpBall->GetVY(), tmpBall->GetVZ(),
188 	   tmpBall->GetSpin(), (int)tmpBall->GetStatus() );
189   Log( LOG_COMBALL, buf );
190 
191   return true;
192 }
193 
194 bool
LogSendPVMessage(Player * player)195 Logging::LogSendPVMessage( Player *player ) {
196   char buf[256];
197 
198   LogTime( LOG_COMTHEPLAYER );
199   snprintf( buf, sizeof(buf),
200            "send PV: x=%4.2f y=%4.2f z=%4.2f vx=%4.2f vy=%4.2f vz=%4.2f\n",
201 	   player->GetX(), player->GetY(), player->GetZ(),
202 	   player->GetVX(), player->GetVY(), player->GetVZ() );
203   Log( LOG_COMTHEPLAYER, buf );
204 
205   return true;
206 }
207 
208 bool
LogRecvPVMessage(ExternalPVData * pv)209 Logging::LogRecvPVMessage( ExternalPVData *pv ) {
210   char buf[256];
211 
212   LogTime( LOG_COMCOMPLAYER );
213   snprintf( buf, sizeof(buf),
214             "recv PV: %d.%3d ", (int)pv->sec, (int)pv->count );
215   Log( LOG_COMCOMPLAYER, buf );
216   Player *tmpPlayer = new Player();
217   tmpPlayer->Warp(pv->data);
218 
219   snprintf( buf, sizeof(buf),
220             "x=%4.2f y=%4.2f z=%4.2f vx=%4.2f vy=%4.2f vz=%4.2f\n",
221             tmpPlayer->GetX(), tmpPlayer->GetY(), tmpPlayer->GetZ(),
222             tmpPlayer->GetVX(), tmpPlayer->GetVY(), tmpPlayer->GetVZ() );
223   Log( LOG_COMCOMPLAYER, buf );
224 
225   return true;
226 }
227 
228 bool
LogSendPSMessage(Player * player)229 Logging::LogSendPSMessage( Player *player ) {
230   char buf[256];
231 
232   LogTime( LOG_COMTHEPLAYER );
233 
234   snprintf( buf, sizeof(buf),
235             "send PS: pow=%2d spin=%3.2f swingType=%1d swingSide=%2d swing=%2d\n",
236 	   (int)player->GetPower(), player->GetSpin(),
237 	   (int)player->GetSwingType(), player->GetSwingSide(),
238 	   (int)player->GetSwing() );
239   Log( LOG_COMTHEPLAYER, buf );
240 
241   return true;
242 }
243 
244 bool
LogRecvPSMessage(ExternalPSData * ps)245 Logging::LogRecvPSMessage( ExternalPSData *ps ) {
246   char buf[256];
247 
248   LogTime( LOG_COMCOMPLAYER );
249   snprintf( buf, sizeof(buf),
250             "recv PS: %d.%3d ", (int)ps->sec, (int)ps->count );
251   Log( LOG_COMCOMPLAYER, buf );
252   Player *tmpPlayer = new Player();
253   tmpPlayer->ExternalSwing(ps->data);
254 
255   snprintf( buf, sizeof(buf),
256            "pow=%2d spin=%3.2f swingType=%1d swingSide=%2d swing=%2d\n",
257 	   (int)tmpPlayer->GetPower(), tmpPlayer->GetSpin(),
258 	   (int)tmpPlayer->GetSwingType(), tmpPlayer->GetSwingSide(),
259 	   (int)tmpPlayer->GetSwing() );
260   Log( LOG_COMCOMPLAYER, buf );
261 
262   return true;
263 }
264 
265 bool
LogRecvPTMessage(ExternalPTData * pt)266 Logging::LogRecvPTMessage( ExternalPTData *pt ) {
267   char buf[256];
268 
269   LogTime( LOG_COMMISC );
270   snprintf( buf, sizeof(buf),
271             "recv PT: %d.%3d ", (int)pt->sec, (int)pt->count );
272   Log( LOG_COMMISC, buf );
273 
274   long rotate;
275   ReadLong( &(pt->data[1]), rotate );
276 
277   snprintf( buf, sizeof(buf), "fixed=%d type=%ld\n", pt->data[0],
278 	    rotate );
279   Log( LOG_COMMISC, buf );
280 
281   return true;
282 }
283 
284 #endif
285