blob: 360307ad15337b851278bd42b95c3e2c79b6e04c [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
24using namespace std;
25using namespace apache::thrift::concurrency;
26
27namespace {
28
James E. King, III36200902016-10-05 14:47:18 -040029// Define environment variable DEBUG_EVENTLOG to enable debug logging
30// ex: $ DEBUG_EVENTLOG=1 processor_test
31static const char * DEBUG_EVENTLOG = getenv("DEBUG_EVENTLOG");
32
Roger Meier2be7f242012-05-10 09:01:45 +000033void debug(const char* fmt, ...) {
James E. King, III36200902016-10-05 14:47:18 -040034 if (DEBUG_EVENTLOG) {
35 va_list ap;
36 va_start(ap, fmt);
37 vfprintf(stderr, fmt, ap);
38 va_end(ap);
Roger Meier2be7f242012-05-10 09:01:45 +000039
James E. King, III36200902016-10-05 14:47:18 -040040 fprintf(stderr, "\n");
41 }
Roger Meier2be7f242012-05-10 09:01:45 +000042}
Roger Meier2be7f242012-05-10 09:01:45 +000043}
44
Konrad Grochowski16a23a62014-11-13 15:33:38 +010045namespace apache {
46namespace thrift {
47namespace test {
Roger Meier2be7f242012-05-10 09:01:45 +000048
49uint32_t EventLog::nextId_ = 0;
50
51#define EVENT_TYPE(value) EventType EventLog::value = #value
52EVENT_TYPE(ET_LOG_END);
53EVENT_TYPE(ET_CONN_CREATED);
54EVENT_TYPE(ET_CONN_DESTROYED);
55EVENT_TYPE(ET_CALL_STARTED);
56EVENT_TYPE(ET_CALL_FINISHED);
57EVENT_TYPE(ET_PROCESS);
58EVENT_TYPE(ET_PRE_READ);
59EVENT_TYPE(ET_POST_READ);
60EVENT_TYPE(ET_PRE_WRITE);
61EVENT_TYPE(ET_POST_WRITE);
62EVENT_TYPE(ET_ASYNC_COMPLETE);
63EVENT_TYPE(ET_HANDLER_ERROR);
64
65EVENT_TYPE(ET_CALL_INCREMENT_GENERATION);
66EVENT_TYPE(ET_CALL_GET_GENERATION);
67EVENT_TYPE(ET_CALL_ADD_STRING);
68EVENT_TYPE(ET_CALL_GET_STRINGS);
69EVENT_TYPE(ET_CALL_GET_DATA_WAIT);
70EVENT_TYPE(ET_CALL_ONEWAY_WAIT);
71EVENT_TYPE(ET_CALL_EXCEPTION_WAIT);
72EVENT_TYPE(ET_CALL_UNEXPECTED_EXCEPTION_WAIT);
73EVENT_TYPE(ET_CALL_SET_VALUE);
74EVENT_TYPE(ET_CALL_GET_VALUE);
75EVENT_TYPE(ET_WAIT_RETURN);
76
77EventLog::EventLog() {
78 id_ = nextId_++;
79 debug("New log: %d", id_);
80}
81
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082void EventLog::append(EventType type,
83 uint32_t connectionId,
84 uint32_t callId,
Roger Meier2be7f242012-05-10 09:01:45 +000085 const string& message) {
86 Synchronized s(monitor_);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 debug("%d <-- %u, %u, %s \"%s\"", id_, connectionId, callId, type, message.c_str());
Roger Meier2be7f242012-05-10 09:01:45 +000088
89 Event e(type, connectionId, callId, message);
90 events_.push_back(e);
91
92 monitor_.notify();
93}
94
95Event EventLog::waitForEvent(int64_t timeout) {
96 Synchronized s(monitor_);
97
98 try {
99 while (events_.empty()) {
100 monitor_.wait(timeout);
101 }
102 } catch (TimedOutException ex) {
103 return Event(ET_LOG_END, 0, 0, "");
104 }
105
106 Event event = events_.front();
107 events_.pop_front();
108 return event;
109}
110
111Event EventLog::waitForConnEvent(uint32_t connId, int64_t timeout) {
112 Synchronized s(monitor_);
113
114 EventList::iterator it = events_.begin();
115 while (true) {
116 try {
117 // TODO: it would be nicer to honor timeout for the duration of this
118 // call, rather than restarting it for each call to wait(). It shouldn't
119 // be a big problem in practice, though.
120 while (it == events_.end()) {
121 monitor_.wait(timeout);
122 }
123 } catch (TimedOutException ex) {
124 return Event(ET_LOG_END, 0, 0, "");
125 }
126
127 if (it->connectionId == connId) {
128 Event event = *it;
129 events_.erase(it);
130 return event;
131 }
132 }
133}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134}
135}
136} // apache::thrift::test