blob: e3ddbccc41ba364e8480f018b5484e53509ef2bd [file] [log] [blame]
Roger Meier2be7f242012-05-10 09:01:45 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Roger Meier2b1a5282012-05-11 10:12:39 +000019#include "EventLog.h"
Roger Meier2be7f242012-05-10 09:01:45 +000020
21#include <stdarg.h>
James E. King, III36200902016-10-05 14:47:18 -040022#include <stdlib.h>
Roger Meier2be7f242012-05-10 09:01:45 +000023
Roger Meier2be7f242012-05-10 09:01:45 +000024using namespace apache::thrift::concurrency;
25
26namespace {
27
James E. King, III36200902016-10-05 14:47:18 -040028// Define environment variable DEBUG_EVENTLOG to enable debug logging
29// ex: $ DEBUG_EVENTLOG=1 processor_test
30static const char * DEBUG_EVENTLOG = getenv("DEBUG_EVENTLOG");
31
Roger Meier2be7f242012-05-10 09:01:45 +000032void debug(const char* fmt, ...) {
James E. King, III36200902016-10-05 14:47:18 -040033 if (DEBUG_EVENTLOG) {
34 va_list ap;
35 va_start(ap, fmt);
36 vfprintf(stderr, fmt, ap);
37 va_end(ap);
Roger Meier2be7f242012-05-10 09:01:45 +000038
James E. King, III36200902016-10-05 14:47:18 -040039 fprintf(stderr, "\n");
40 }
Roger Meier2be7f242012-05-10 09:01:45 +000041}
Roger Meier2be7f242012-05-10 09:01:45 +000042}
43
Konrad Grochowski16a23a62014-11-13 15:33:38 +010044namespace apache {
45namespace thrift {
46namespace test {
Roger Meier2be7f242012-05-10 09:01:45 +000047
48uint32_t EventLog::nextId_ = 0;
49
50#define EVENT_TYPE(value) EventType EventLog::value = #value
51EVENT_TYPE(ET_LOG_END);
52EVENT_TYPE(ET_CONN_CREATED);
53EVENT_TYPE(ET_CONN_DESTROYED);
54EVENT_TYPE(ET_CALL_STARTED);
55EVENT_TYPE(ET_CALL_FINISHED);
56EVENT_TYPE(ET_PROCESS);
57EVENT_TYPE(ET_PRE_READ);
58EVENT_TYPE(ET_POST_READ);
59EVENT_TYPE(ET_PRE_WRITE);
60EVENT_TYPE(ET_POST_WRITE);
61EVENT_TYPE(ET_ASYNC_COMPLETE);
62EVENT_TYPE(ET_HANDLER_ERROR);
63
64EVENT_TYPE(ET_CALL_INCREMENT_GENERATION);
65EVENT_TYPE(ET_CALL_GET_GENERATION);
66EVENT_TYPE(ET_CALL_ADD_STRING);
67EVENT_TYPE(ET_CALL_GET_STRINGS);
68EVENT_TYPE(ET_CALL_GET_DATA_WAIT);
69EVENT_TYPE(ET_CALL_ONEWAY_WAIT);
70EVENT_TYPE(ET_CALL_EXCEPTION_WAIT);
71EVENT_TYPE(ET_CALL_UNEXPECTED_EXCEPTION_WAIT);
72EVENT_TYPE(ET_CALL_SET_VALUE);
73EVENT_TYPE(ET_CALL_GET_VALUE);
74EVENT_TYPE(ET_WAIT_RETURN);
75
76EventLog::EventLog() {
77 id_ = nextId_++;
78 debug("New log: %d", id_);
79}
80
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081void EventLog::append(EventType type,
82 uint32_t connectionId,
83 uint32_t callId,
James E. King, III82ae9572017-08-05 12:23:54 -040084 const std::string& message) {
Roger Meier2be7f242012-05-10 09:01:45 +000085 Synchronized s(monitor_);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086 debug("%d <-- %u, %u, %s \"%s\"", id_, connectionId, callId, type, message.c_str());
Roger Meier2be7f242012-05-10 09:01:45 +000087
88 Event e(type, connectionId, callId, message);
89 events_.push_back(e);
90
91 monitor_.notify();
92}
93
94Event EventLog::waitForEvent(int64_t timeout) {
95 Synchronized s(monitor_);
96
97 try {
98 while (events_.empty()) {
99 monitor_.wait(timeout);
100 }
101 } catch (TimedOutException ex) {
102 return Event(ET_LOG_END, 0, 0, "");
103 }
104
105 Event event = events_.front();
106 events_.pop_front();
107 return event;
108}
109
110Event EventLog::waitForConnEvent(uint32_t connId, int64_t timeout) {
111 Synchronized s(monitor_);
112
113 EventList::iterator it = events_.begin();
114 while (true) {
115 try {
116 // TODO: it would be nicer to honor timeout for the duration of this
117 // call, rather than restarting it for each call to wait(). It shouldn't
118 // be a big problem in practice, though.
119 while (it == events_.end()) {
120 monitor_.wait(timeout);
121 }
122 } catch (TimedOutException ex) {
123 return Event(ET_LOG_END, 0, 0, "");
124 }
125
126 if (it->connectionId == connId) {
127 Event event = *it;
128 events_.erase(it);
129 return event;
130 }
131 }
132}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100133}
134}
135} // apache::thrift::test