MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
log.cpp
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* Debug logging functions
20  */
21 
22 
23 #include "runtime.hpp"
24 #include "log.hpp"
25 
26 #ifdef YASSL_LOG
27  #include <time.h>
28  #include <stdio.h>
29  #include <string.h>
30 #endif
31 
32 
33 
34 namespace yaSSL {
35 
36 
37 #ifdef YASSL_LOG
38 
39  enum { MAX_MSG = 81 };
40 
41  Log::Log(const char* str)
42  {
43  log_ = fopen(str, "w");
44  Trace("********** Logger Attached **********");
45  }
46 
47 
48  Log::~Log()
49  {
50  Trace("********** Logger Detached **********");
51  fclose(log_);
52  }
53 
54 
55  // Trace a message
56  void Log::Trace(const char* str)
57  {
58  if (!log_) return;
59 
60  time_t clicks = time(0);
61  char timeStr[32];
62 
63  // get rid of newline
64  strncpy(timeStr, ctime(&clicks), sizeof(timeStr));
65  unsigned int len = strlen(timeStr);
66  timeStr[len - 1] = 0;
67 
68  char msg[MAX_MSG];
69 
70  strncpy(msg, timeStr, sizeof(timeStr));
71  strncat(msg, ":", 1);
72  strncat(msg, str, MAX_MSG - sizeof(timeStr) - 2);
73  strncat(msg, "\n", 1);
74  msg[MAX_MSG - 1] = 0;
75 
76  fputs(msg, log_);
77  }
78 
79 
80  #if defined(_WIN32) || defined(__MACH__) || defined(__hpux__)
81  typedef int socklen_t;
82  #endif
83 
84 
85  // write tcp address
86  void Log::ShowTCP(socket_t fd, bool ended)
87  {
88  sockaddr_in peeraddr;
89  socklen_t len = sizeof(peeraddr);
90  if (getpeername(fd, (sockaddr*)&peeraddr, &len) != 0)
91  return;
92 
93  const char* p = reinterpret_cast<const char*>(&peeraddr.sin_addr);
94  char msg[MAX_MSG];
95  char number[16];
96 
97  if (ended)
98  strncpy(msg, "yaSSL conn DONE w/ peer ", 26);
99  else
100  strncpy(msg, "yaSSL conn BEGUN w/ peer ", 26);
101  for (int i = 0; i < 4; ++i) {
102  sprintf(number, "%u", static_cast<unsigned short>(p[i]));
103  strncat(msg, number, 8);
104  if (i < 3)
105  strncat(msg, ".", 1);
106  }
107  strncat(msg, " port ", 8);
108  sprintf(number, "%d", htons(peeraddr.sin_port));
109  strncat(msg, number, 8);
110 
111  msg[MAX_MSG - 1] = 0;
112  Trace(msg);
113  }
114 
115 
116  // log processed data
117  void Log::ShowData(uint bytes, bool sent)
118  {
119  char msg[MAX_MSG];
120  char number[16];
121 
122  if (sent)
123  strncpy(msg, "Sent ", 10);
124  else
125  strncpy(msg, "Received ", 10);
126  sprintf(number, "%u", bytes);
127  strncat(msg, number, 8);
128  strncat(msg, " bytes of application data", 27);
129 
130  msg[MAX_MSG - 1] = 0;
131  Trace(msg);
132  }
133 
134 
135 #else // no YASSL_LOG
136 
137 
138  Log::Log(const char*) {}
139  Log::~Log() {}
140  void Log::Trace(const char*) {}
141  void Log::ShowTCP(socket_t, bool) {}
142  void Log::ShowData(uint, bool) {}
143 
144 
145 #endif // YASSL_LOG
146 } // namespace