root/trunk/src/shared/PacketLog.cpp @ 8

Revision 2, 4.1 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include "Common.h"
20#include "PacketLog.h"
21#include "Config/ConfigEnv.h"
22#include "Policies/SingletonImpl.h"
23
24#include <ctype.h>
25
26INSTANTIATE_SINGLETON_1( PacketLog );
27
28PacketLog::PacketLog()
29{
30
31    if (sConfig.GetBoolDefault("LogRealm", false))
32    {
33        FILE *pFile = fopen("realm.log", "w+");
34        fclose(pFile);
35    }
36
37    if (sConfig.GetBoolDefault("LogWorld", false))
38    {
39        FILE *pFile = fopen("world.log", "w+");
40        fclose(pFile);
41    }
42}
43
44PacketLog::~PacketLog()
45{
46}
47
48char PacketLog::makehexchar(int i)
49{
50    return (i<=9) ? '0'+i : 'A'+(i-10);
51}
52
53int PacketLog::hextoint(char c)
54{
55    c = toupper(c);
56    return (c > '9' ? c - 'A' + 10 : c - '0');
57}
58
59void PacketLog::HexDump(const unsigned char* data, size_t length, const char* file)
60{
61    FILE *pFile;
62    pFile = fopen(file, "a");
63
64    const int char_offset = 16*3 + 2;
65    const int line_size = 16*3 + 16 + 3;
66    char line[line_size+1];
67
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");
70
71    line[char_offset - 1] = ' ';
72    line[char_offset - 2] = ' ';
73
74    for (size_t i=0; i<length; )
75    {
76        int bi=0;
77        int ci=0;
78
79        int start_i = i;
80
81        for (int line_i=0; i < length && line_i < 16; i++, line_i++)
82        {
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;
88        }
89
90        while (bi<16*3)
91        {
92            line[bi++]=' ';
93        }
94
95        line[char_offset+(ci++)]='\n';
96        line[char_offset+ci]=0;
97
98        fprintf(pFile,"%06X  %s", start_i, line);
99    }
100    fprintf(pFile, "\n\n");
101    fclose(pFile);
102}
103
104void PacketLog::HexDump(const char *data, size_t length, const char* file)
105{
106    HexDump((unsigned char *)data, length, file);
107}
108
109void PacketLog::HexDumpStr(const char *msg, const char *data, size_t len, const char* file)
110{
111    FILE *pFile;
112    pFile = fopen(file, "a");
113    fprintf(pFile,"%s\n", msg);
114    fclose(pFile);
115
116    HexDump(data, len, file);
117}
118
119void PacketLog::RealmHexDump(RealmPacket* data, uint32 socket, bool direction)
120{
121    if (!sConfig.GetBoolDefault("LogRealm", false))
122        return;
123
124    FILE *pFile;
125    pFile = fopen("realm.log", "a");
126
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);
133
134    fclose(pFile);
135    HexDump((char *)data->contents(), data->size(), "realm.log");
136
137}
138
139void PacketLog::WorldHexDump(WorldPacket* data, uint32 socket, bool direction)
140{
141    if (!sConfig.GetBoolDefault("LogWorld", false))
142        return;
143
144    FILE *pFile;
145    pFile = fopen("world.log", "a");
146
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);
153
154    fclose(pFile);
155    HexDump((char *)data->contents(), data->size(), "world.log");
156
157}
Note: See TracBrowser for help on using the browser.