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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <cassert>
#include <csignal>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <locale>
#include <map>
#include <sstream>
#include <vector>
#include "eckit/config/LibEcKit.h"
#include "eckit/filesystem/LocalPathName.h"
#include "eckit/os/BackTrace.h"
#include "eckit/runtime/Tool.h"
#include "eckit/log/CallbackTarget.h"
#include "eckit/log/Channel.h"
#include "eckit/log/ColouringTarget.h"
#include "eckit/log/FileTarget.h"
#include "eckit/log/OStreamTarget.h"
#include "eckit/log/WrapperTarget.h"
#include "eckit/testing/Test.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
//----------------------------------------------------------------------------------------------------------------------
#if 1
#define DEBUG_H
#define DEBUG_(x)
#else
#define DEBUG_H std::cerr << " DEBUG @ " << __FILE__ << " +" << __LINE__ << std::endl;
#define DEBUG_(x) std::cerr << #x << " : [" << x << "] @ " << __FILE__ << " +" << __LINE__ << std::endl;
#endif
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
/// Example of a wrapper target that capitalizes all output
class CapitalizerTarget : public WrapperTarget {
public:
CapitalizerTarget(LogTarget* target) : WrapperTarget(target) {}
private:
virtual void write(const char* start, const char* end) {
std::string::size_type length = std::distance(start, end);
buffer_.resize(length);
std::locale loc;
const char* p = start;
for (std::string::size_type i = 0; i < length; ++i, ++p) {
buffer_[i] = std::toupper(*p, loc);
}
target_->write(buffer_.c_str(), buffer_.c_str() + length);
}
virtual void writePrefix() {}
virtual void writeSuffix() {}
void print(std::ostream& s) const { s << "CapitalizerTarget()"; }
std::string buffer_;
};
//----------------------------------------------------------------------------------------------------------------------
static void callback_ctxt(void* ctxt, const char* msg) {
std::cout << "[" << *((int*)ctxt) << "] : -- " << msg << std::endl;
}
static void callback_noctxt(void*, const char* msg) {
std::cout << "[CALLBACK OUT] : -- " << msg << std::endl;
}
//----------------------------------------------------------------------------------------------------------------------
CASE("test_multi_targets") {
std::cout << "---> test_multi_targets()" << std::endl;
int t = 0;
Channel mychannel;
mychannel << "testing [" << t++ << "]" << std::endl;
mychannel.addFile("test.txt");
mychannel << "testing [" << t++ << "]" << std::endl;
std::ofstream of("test.txt.2");
mychannel.addStream(of);
mychannel << "testing [" << t++ << "]" << std::endl;
mychannel.addStream(std::cout);
mychannel << "testing [" << t++ << "]" << std::endl;
mychannel.addStream(std::cerr);
mychannel << "testing [" << t++ << "]" << std::endl;
std::ostringstream oss;
mychannel.addStream(oss);
mychannel << "testing [" << t++ << "]" << std::endl;
mychannel.addCallback(&callback_noctxt, nullptr);
mychannel.addCallback(&callback_ctxt, &t);
mychannel << "testing [" << t++ << "]" << std::endl;
// mychannel.addLogTarget(new CapitalizerTarget(new FileTarget(PathName("capitals.txt"))));
mychannel << "testing [" << t++ << "]" << std::endl;
mychannel << "Final test" << std::endl;
}
CASE("test_multi_colouring") {
#if 0
Log::info().setLogTarget( new ColouringTarget(new OStreamTarget(std::cout), &Colour::green));
Log::warning().setLogTarget( new ColouringTarget(new OStreamTarget(std::cerr), &Colour::yellow));
Log::error().setLogTarget( new ColouringTarget(new OStreamTarget(std::cerr), &Colour::red));
Log::info() << "Log::info() is green" << std::endl;
Log::warning() << "Log::warning() is yellow" << std::endl;
Log::error() << "Log::error() is red" << std::endl;
#endif
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
void on_signal_dumpbacktrace(int signum) {
printf("Caught signal %d\n", signum);
std::cerr << BackTrace::dump() << std::endl;
eckit::LibEcKit::instance().abort();
}
int main(int argc, char** argv) {
signal(SIGSEGV, on_signal_dumpbacktrace);
return run_tests(argc, argv);
}
|