2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "PacketLog.h"
21 #include "Config/ConfigEnv.h"
22 #include "Policies/SingletonImpl.h"
26 INSTANTIATE_SINGLETON_1( PacketLog
);
28 PacketLog::PacketLog()
31 if (sConfig
.GetBoolDefault("LogRealm", false))
33 FILE *pFile
= fopen("realm.log", "w+");
37 if (sConfig
.GetBoolDefault("LogWorld", false))
39 FILE *pFile
= fopen("world.log", "w+");
44 PacketLog::~PacketLog()
48 char PacketLog::makehexchar(int i
)
50 return (i
<=9) ? '0'+i
: 'A'+(i
-10);
53 int PacketLog::hextoint(char c
)
56 return (c
> '9' ? c
- 'A' + 10 : c
- '0');
59 void PacketLog::HexDump(const unsigned char* data
, size_t length
, const char* file
)
62 pFile
= fopen(file
, "a");
64 const int char_offset
= 16*3 + 2;
65 const int line_size
= 16*3 + 16 + 3;
66 char line
[line_size
+1];
68 fprintf(pFile
,"OFFSET 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF\n");
69 fprintf(pFile
,"--------------------------------------------------------------------------\n");
71 line
[char_offset
- 1] = ' ';
72 line
[char_offset
- 2] = ' ';
74 for (size_t i
=0; i
<length
; )
81 for (int line_i
=0; i
< length
&& line_i
< 16; i
++, line_i
++)
83 line
[bi
++] = makehexchar(*data
>>4);
84 line
[bi
++] = makehexchar(*data
& 0x0f);
86 line
[char_offset
+(ci
++)]=(isprint(*data
) ? *data
: '.');
95 line
[char_offset
+(ci
++)]='\n';
96 line
[char_offset
+ci
]=0;
98 fprintf(pFile
,"%06X %s", start_i
, line
);
100 fprintf(pFile
, "\n\n");
104 void PacketLog::HexDump(const char *data
, size_t length
, const char* file
)
106 HexDump((unsigned char *)data
, length
, file
);
109 void PacketLog::HexDumpStr(const char *msg
, const char *data
, size_t len
, const char* file
)
112 pFile
= fopen(file
, "a");
113 fprintf(pFile
,"%s\n", msg
);
116 HexDump(data
, len
, file
);
119 void PacketLog::RealmHexDump(RealmPacket
* data
, uint32 socket
, bool direction
)
121 if (!sConfig
.GetBoolDefault("LogRealm", false))
125 pFile
= fopen("realm.log", "a");
127 uint16 len
= data
->size() + 2;
128 uint8 opcode
= data
->GetOpcode();
130 fprintf(pFile
, "SERVER:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.2X\nDATA:\n", socket
, len
, opcode
);
132 fprintf(pFile
, "CLIENT:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.2X\nDATA:\n", socket
, len
, opcode
);
135 HexDump((char *)data
->contents(), data
->size(), "realm.log");
139 void PacketLog::WorldHexDump(WorldPacket
* data
, uint32 socket
, bool direction
)
141 if (!sConfig
.GetBoolDefault("LogWorld", false))
145 pFile
= fopen("world.log", "a");
147 uint16 len
= data
->size();
148 uint16 opcode
= data
->GetOpcode();
150 fprintf(pFile
, "SERVER:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.4X\nDATA:\n", socket
, len
, opcode
);
152 fprintf(pFile
, "CLIENT:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.4X\nDATA:\n", socket
, len
, opcode
);
155 HexDump((char *)data
->contents(), data
->size(), "world.log");