-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathats_message_handler.cc
116 lines (95 loc) · 3.32 KB
/
ats_message_handler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ats_message_handler.h"
#include <signal.h>
#include <unistd.h>
#include "pagespeed/kernel/base/abstract_mutex.h"
#include "pagespeed/kernel/base/debug.h"
#include "pagespeed/kernel/sharedmem/shared_circular_buffer.h"
#include "pagespeed/kernel/base/string_util.h"
#include "net/instaweb/public/version.h"
#include "pagespeed/kernel/base/posix_timer.h"
#include "pagespeed/kernel/base/time_util.h"
namespace
{
// This will be prefixed to every logged message.
const char kModuleName[] = "ats_pagespeed";
} // namespace
namespace net_instaweb
{
AtsMessageHandler::AtsMessageHandler(AbstractMutex *mutex) : mutex_(mutex), buffer_(NULL)
{
SetPidString(static_cast<int64>(getpid()));
}
bool
AtsMessageHandler::Dump(Writer *writer)
{
// Can't dump before SharedCircularBuffer is set up.
if (buffer_ == NULL) {
return false;
}
return buffer_->Dump(writer, &handler_);
}
void
AtsMessageHandler::set_buffer(SharedCircularBuffer *buff)
{
ScopedMutex lock(mutex_.get());
buffer_ = buff;
}
void
AtsMessageHandler::MessageVImpl(MessageType type, const char *msg, va_list args)
{
GoogleString formatted_message = Format(msg, args);
TSDebug("ats_pagespeed", "[%s %s] %s", kModuleName, kModPagespeedVersion, formatted_message.c_str());
// Prepare a log message for the SharedCircularBuffer only.
// Prepend time and severity to message.
// Format is [time] [severity] [pid] message.
GoogleString message;
GoogleString time;
PosixTimer timer;
if (!ConvertTimeToString(timer.NowMs(), &time)) {
time = "?";
}
StrAppend(&message, "[", time, "] ", "[", MessageTypeToString(type), "] ");
StrAppend(&message, pid_string_, " ", formatted_message, "\n");
{
ScopedMutex lock(mutex_.get());
if (buffer_ != NULL) {
const StringPiece msg(message);
// NULL will do for now
buffer_->Write(msg,NULL);
}
}
}
void
AtsMessageHandler::FileMessageVImpl(MessageType type, const char *file, int line, const char *msg, va_list args)
{
GoogleString formatted_message = Format(msg, args);
TSDebug("ats_pagespeed", "[%s %s] %s:%d:%s", kModuleName, kModPagespeedVersion, file, line, formatted_message.c_str());
}
// TODO(sligocki): It'd be nice not to do so much string copying.
GoogleString
AtsMessageHandler::Format(const char *msg, va_list args)
{
GoogleString buffer;
// Ignore the name of this routine: it formats with vsnprintf.
// See base/stringprintf.cc.
StringAppendV(&buffer, msg, args);
return buffer;
}
} // namespace net_instaweb