~vlad-lesin/percona-server/mysql-5.0.33-original

« back to all changes in this revision

Viewing changes to extra/yassl/src/log.cpp

  • Committer: Vlad Lesin
  • Date: 2012-07-31 09:21:34 UTC
  • Revision ID: vladislav.lesin@percona.com-20120731092134-zfodx022b7992wsi
VirginĀ 5.0.33

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* log.cpp
 
2
 *
 
3
 * Copyright (C) 2003 Sawtooth Consulting Ltd.
 
4
 *
 
5
 * This file is part of yaSSL.
 
6
 *
 
7
 * yaSSL is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * There are special exceptions to the terms and conditions of the GPL as it
 
13
 * is applied to yaSSL. View the full text of the exception in the file
 
14
 * FLOSS-EXCEPTIONS in the directory of this software distribution.
 
15
 *
 
16
 * yaSSL is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
24
 */
 
25
 
 
26
/*  Debug logging functions
 
27
 */
 
28
 
 
29
 
 
30
#include "runtime.hpp"
 
31
#include "log.hpp"
 
32
 
 
33
#ifdef YASSL_LOG
 
34
    #include <time.h>
 
35
    #include <stdio.h>
 
36
    #include <string.h>
 
37
#endif
 
38
 
 
39
 
 
40
 
 
41
namespace yaSSL {
 
42
 
 
43
 
 
44
#ifdef YASSL_LOG
 
45
 
 
46
    enum { MAX_MSG = 81 };
 
47
 
 
48
    Log::Log(const char* str)
 
49
    {
 
50
        log_ = fopen(str, "w");
 
51
        Trace("********** Logger Attached **********");
 
52
    }
 
53
 
 
54
 
 
55
    Log::~Log()
 
56
    {
 
57
        Trace("********** Logger Detached **********");
 
58
        fclose(log_);
 
59
    }
 
60
 
 
61
 
 
62
    // Trace a message
 
63
    void Log::Trace(const char* str)
 
64
    {
 
65
        if (!log_) return;
 
66
 
 
67
        time_t clicks = time(0);
 
68
        char   timeStr[32];
 
69
 
 
70
        // get rid of newline
 
71
        strncpy(timeStr, ctime(&clicks), sizeof(timeStr));
 
72
        unsigned int len = strlen(timeStr);
 
73
        timeStr[len - 1] = 0;
 
74
 
 
75
        char msg[MAX_MSG];
 
76
 
 
77
        strncpy(msg, timeStr, sizeof(timeStr));
 
78
        strncat(msg, ":", 1);
 
79
        strncat(msg, str, MAX_MSG - sizeof(timeStr) - 2);
 
80
        strncat(msg, "\n", 1);
 
81
        msg[MAX_MSG - 1] = 0;
 
82
 
 
83
        fputs(msg, log_);
 
84
    }
 
85
 
 
86
 
 
87
    #if defined(_WIN32) || defined(__MACH__) || defined(__hpux__)
 
88
        typedef int socklen_t;
 
89
    #endif
 
90
 
 
91
 
 
92
    // write tcp address
 
93
    void Log::ShowTCP(socket_t fd, bool ended)
 
94
    {
 
95
        sockaddr_in peeraddr;
 
96
        socklen_t   len = sizeof(peeraddr);
 
97
        if (getpeername(fd, (sockaddr*)&peeraddr, &len) != 0)
 
98
            return;
 
99
 
 
100
        const char* p = reinterpret_cast<const char*>(&peeraddr.sin_addr);
 
101
        char msg[MAX_MSG];
 
102
        char number[16];
 
103
    
 
104
        if (ended)
 
105
            strncpy(msg, "yaSSL conn DONE  w/ peer ", 26);
 
106
        else
 
107
            strncpy(msg, "yaSSL conn BEGUN w/ peer ", 26);
 
108
        for (int i = 0; i < 4; ++i) {
 
109
            sprintf(number, "%u", static_cast<unsigned short>(p[i]));
 
110
            strncat(msg, number, 8);
 
111
            if (i < 3)
 
112
                strncat(msg, ".", 1);
 
113
        }
 
114
        strncat(msg, " port ", 8);
 
115
        sprintf(number, "%d", htons(peeraddr.sin_port));
 
116
        strncat(msg, number, 8);
 
117
 
 
118
        msg[MAX_MSG - 1] = 0;
 
119
        Trace(msg);
 
120
    }
 
121
 
 
122
 
 
123
    // log processed data
 
124
    void Log::ShowData(uint bytes, bool sent)
 
125
    {
 
126
        char msg[MAX_MSG];
 
127
        char number[16];
 
128
 
 
129
        if (sent)
 
130
            strncpy(msg, "Sent     ", 10); 
 
131
        else
 
132
            strncpy(msg, "Received ", 10);
 
133
        sprintf(number, "%u", bytes);
 
134
        strncat(msg, number, 8);
 
135
        strncat(msg, " bytes of application data", 27);
 
136
 
 
137
        msg[MAX_MSG - 1] = 0;
 
138
        Trace(msg);
 
139
    }
 
140
 
 
141
 
 
142
#else // no YASSL_LOG
 
143
 
 
144
 
 
145
    Log::Log(const char*) {}
 
146
    Log::~Log() {}
 
147
    void Log::Trace(const char*) {}
 
148
    void Log::ShowTCP(socket_t, bool) {}
 
149
    void Log::ShowData(uint, bool) {}
 
150
 
 
151
 
 
152
#endif // YASSL_LOG
 
153
} // namespace