Merge branch 'master' into 303
[getmangos.git] / src / shared / PacketLog.cpp
blob06e19bd979a9842f93fe886e751af2a5806306b7
1 /*
2 * Copyright (C) 2005-2008 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
19 #include "Common.h"
20 #include "PacketLog.h"
21 #include "Config/ConfigEnv.h"
22 #include "Policies/SingletonImpl.h"
24 #include <ctype.h>
26 INSTANTIATE_SINGLETON_1( PacketLog );
28 PacketLog::PacketLog()
31 if (sConfig.GetBoolDefault("LogRealm", false))
33 FILE *pFile = fopen("realm.log", "w+");
34 fclose(pFile);
37 if (sConfig.GetBoolDefault("LogWorld", false))
39 FILE *pFile = fopen("world.log", "w+");
40 fclose(pFile);
44 PacketLog::~PacketLog()
48 char PacketLog::makehexchar(int i)
50 return (i<=9) ? '0'+i : 'A'+(i-10);
53 int PacketLog::hextoint(char c)
55 c = toupper(c);
56 return (c > '9' ? c - 'A' + 10 : c - '0');
59 void PacketLog::HexDump(const unsigned char* data, size_t length, const char* file)
61 FILE *pFile;
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; )
76 int bi=0;
77 int ci=0;
79 int start_i = i;
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);
85 line[bi++] = ' ';
86 line[char_offset+(ci++)]=(isprint(*data) ? *data : '.');
87 ++data;
90 while (bi<16*3)
92 line[bi++]=' ';
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");
101 fclose(pFile);
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)
111 FILE *pFile;
112 pFile = fopen(file, "a");
113 fprintf(pFile,"%s\n", msg);
114 fclose(pFile);
116 HexDump(data, len, file);
119 void PacketLog::RealmHexDump(RealmPacket* data, uint32 socket, bool direction)
121 if (!sConfig.GetBoolDefault("LogRealm", false))
122 return;
124 FILE *pFile;
125 pFile = fopen("realm.log", "a");
127 uint16 len = data->size() + 2;
128 uint8 opcode = data->GetOpcode();
129 if (direction)
130 fprintf(pFile, "SERVER:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.2X\nDATA:\n", socket, len, opcode);
131 else
132 fprintf(pFile, "CLIENT:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.2X\nDATA:\n", socket, len, opcode);
134 fclose(pFile);
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))
142 return;
144 FILE *pFile;
145 pFile = fopen("world.log", "a");
147 uint16 len = data->size();
148 uint16 opcode = data->GetOpcode();
149 if (direction)
150 fprintf(pFile, "SERVER:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.4X\nDATA:\n", socket, len, opcode);
151 else
152 fprintf(pFile, "CLIENT:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.4X\nDATA:\n", socket, len, opcode);
154 fclose(pFile);
155 HexDump((char *)data->contents(), data->size(), "world.log");