Browse Source

Improved Logging, prefixed with thread_id now. (DEBUG build only)

added pEpErr() (DEBUG and RELEASE build)
non-local vars names with len == 1 are not handy at all
sync
heck 5 years ago
parent
commit
15a20035eb
  1. 26
      Adapter.cc
  2. 1
      Adapter.hh
  3. 6
      Adapter.hxx
  4. 16
      test/test_adapter.cc
  5. 3
      test/test_adapter_cxx.cc
  6. 7
      utils.hh

26
Adapter.cc

@ -6,13 +6,15 @@
#include <iomanip> #include <iomanip>
#include <assert.h> #include <assert.h>
#include "status_to_string.hh" #include "status_to_string.hh"
#include "utils.hh"
using namespace std; using namespace std;
namespace pEp { namespace pEp {
void throw_status(PEP_STATUS status) void throw_status(PEP_STATUS status)
{ {
pEpLog("called"); //pEpLog("called");
if (status == PEP_STATUS_OK) if (status == PEP_STATUS_OK)
return; return;
if (status >= 0x400 && status <= 0x4ff) if (status >= 0x400 && status <= 0x4ff)
@ -38,21 +40,24 @@ namespace pEp {
std::thread *_sync_thread = nullptr; std::thread *_sync_thread = nullptr;
::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > sync_q; ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > sync_q;
std::mutex m; std::mutex mutex_global;
int _inject_sync_event(SYNC_EVENT ev, void *management) int _inject_sync_event(SYNC_EVENT ev, void *management)
{ {
pEpLog("called"); pEpLog("called");
try { try {
if (ev == nullptr) { if (ev == nullptr) {
pEpLog("SYNC_EVENT: NULL");
sync_q.clear(); sync_q.clear();
sync_q.push_back(ev); sync_q.push_back(ev);
} }
else { else {
pEpLog("SYNC_EVENT:" << ev);
sync_q.push_front(ev); sync_q.push_front(ev);
} }
} }
catch (exception&) { catch (exception&) {
pEpErr("Exception");
return 1; return 1;
} }
if (ev == nullptr) { if (ev == nullptr) {
@ -80,15 +85,18 @@ namespace pEp {
SYNC_EVENT syncEvent = nullptr; SYNC_EVENT syncEvent = nullptr;
const bool success = sync_q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); const bool success = sync_q.try_pop_front(syncEvent, std::chrono::seconds(threshold));
if (!success) if (!success) {
pEpLog("timeout after [sec]: " << threshold);
return new_sync_timeout_event(); return new_sync_timeout_event();
}
pEpLog("returning SYNC_EVENT: " << syncEvent);
return syncEvent; return syncEvent;
} }
bool on_sync_thread() bool on_sync_thread()
{ {
pEpLog("called"); //pEpLog("called");
if (_sync_thread && _sync_thread->get_id() == this_thread::get_id()) if (_sync_thread && _sync_thread->get_id() == this_thread::get_id())
return true; return true;
else else
@ -98,7 +106,7 @@ namespace pEp {
PEP_SESSION session(session_action action) PEP_SESSION session(session_action action)
{ {
pEpLog("called"); pEpLog("called");
std::lock_guard<mutex> lock(m); std::lock_guard<mutex> lock(mutex_global);
bool in_sync = on_sync_thread(); bool in_sync = on_sync_thread();
thread_local static PEP_SESSION _session = nullptr; thread_local static PEP_SESSION _session = nullptr;
@ -107,14 +115,19 @@ namespace pEp {
switch (action) { switch (action) {
case release: case release:
if (_session) { if (_session) {
pEpLog("action = release: releasing session: " << _session);
::release(_session); ::release(_session);
_session = nullptr; _session = nullptr;
} else {
pEpLog("action = release: No session to release");
} }
break; break;
case init: case init:
if (!_session) if (!_session) {
pEpLog("action = init: creating new session");
status = ::init(&_session, _messageToSend, _inject_sync_event); status = ::init(&_session, _messageToSend, _inject_sync_event);
}
break; break;
default: default:
@ -122,6 +135,7 @@ namespace pEp {
} }
throw_status(status); throw_status(status);
pEpLog("returning session: " << _session);
return _session; return _session;
} }

1
Adapter.hh

@ -7,7 +7,6 @@
#include <string> #include <string>
#include <pEp/sync_api.h> #include <pEp/sync_api.h>
#include <stdexcept> #include <stdexcept>
#include "utils.hh"
namespace pEp { namespace pEp {

6
Adapter.hxx

@ -6,6 +6,7 @@
#include <thread> #include <thread>
#include "locked_queue.hh" #include "locked_queue.hh"
#include <cassert> #include <cassert>
#include "utils.hh"
namespace pEp { namespace pEp {
namespace Adapter { namespace Adapter {
@ -16,7 +17,7 @@ namespace pEp {
extern std::thread *_sync_thread; extern std::thread *_sync_thread;
extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q; extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q;
extern std::mutex m; extern std::mutex mutex_global;
SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold); SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold);
@ -25,6 +26,7 @@ namespace pEp {
template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown) template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown)
{ {
pEpLog("called");
_ex = nullptr; _ex = nullptr;
assert(_messageToSend); assert(_messageToSend);
assert(_notifyHandshake); assert(_notifyHandshake);
@ -67,6 +69,8 @@ namespace pEp {
) )
throw (RuntimeError) throw (RuntimeError)
{ {
pEpLog("called");
if (messageToSend) if (messageToSend)
_messageToSend = messageToSend; _messageToSend = messageToSend;

16
test/test_adapter.cc

@ -1,24 +1,25 @@
// This file is under GNU General Public License 3.0 // This file is under GNU General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
#include "Adapter.hh"
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include "Adapter.hh"
#include "../utils.hh"
using namespace std; using namespace std;
using namespace pEp::Adapter; using namespace pEp::Adapter;
PEP_STATUS messageToSend(struct _message *msg) PEP_STATUS messageToSend(struct _message *msg)
{ {
pEpLog("called()"); pEpLog("called");
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
{ {
pEpLog("called()"); pEpLog("called");
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
@ -33,17 +34,16 @@ int main()
pEp::throw_status(status); pEp::throw_status(status);
// start and stop sync repeatedly // start and stop sync repeatedly
useconds_t sleepuSec = 1000 * 100; useconds_t sleepuSec = 1000 * 1000;
unsigned long long int nrIters = 1000 * 1000 * 1000; unsigned long long int nrIters = 3;//1000 * 1000 * 1000;
for (int i = 0; i < nrIters; i++) { for (int i = 0; i < nrIters; i++) {
pEpLog("RUN NR: "); pEpLog("RUN NR: " << i);
pEpLog(i);
pEpLog("SYNC START"); pEpLog("SYNC START");
pEpLog("starting the adapter including sync");
startup(messageToSend, notifyHandshake); startup(messageToSend, notifyHandshake);
pEpLog("SYNC STOP"); pEpLog("SYNC STOP");
usleep(sleepuSec); usleep(sleepuSec);
shutdown(); shutdown();
usleep(sleepuSec);
} }
return 0; return 0;
} }

3
test/test_adapter_cxx.cc

@ -1,11 +1,12 @@
// This file is under GNU General Public License 3.0 // This file is under GNU General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
#include "Adapter.hh"
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include "Adapter.hh"
#include "../utils.hh"
using namespace pEp::Adapter; using namespace pEp::Adapter;

7
utils.hh

@ -1,9 +1,12 @@
#pragma once #pragma once
#include <iostream>
#include <thread>
#ifdef NDEBUG #ifdef NDEBUG
#define pEpLog(msg) do{}while(0) #define pEpLog(msg) do{}while(0)
#else #else
#include <iostream> #define pEpLog(msg) do{std::cout << "Thread:" << std::this_thread::get_id() << ' ' <<__FILE__ << "::" << __FUNCTION__ << " - " << msg << '\n';} while(0)
#define pEpLog(msg) do{std::cerr << __FILE__ << "::" << __FUNCTION__ << " - " << msg << '\n';} while(0)
#endif #endif
#define pEpErr(msg) do{std::cerr << "Thread:" << std::this_thread::get_id() << ' ' <<__FILE__ << "::" << __FUNCTION__ << " - " << msg << '\n';} while(0)
Loading…
Cancel
Save