diff options
author | Rafael H. Schloming <rhs@apache.org> | 2006-09-19 22:06:50 +0000 |
---|---|---|
committer | Rafael H. Schloming <rhs@apache.org> | 2006-09-19 22:06:50 +0000 |
commit | 913489deb2ee9dbf44455de5f407ddaf4bd8c540 (patch) | |
tree | 7ea442d6867d0076f1c9ea4f4265664059e7aff5 /cpp/common/utils | |
download | qpid-python-913489deb2ee9dbf44455de5f407ddaf4bd8c540.tar.gz |
Import of qpid from etp:
URL: https://etp.108.redhat.com/svn/etp/trunk/blaze
Repository Root: https://etp.108.redhat.com/svn/etp
Repository UUID: 06e15bec-b515-0410-bef0-cc27a458cf48
Revision: 608
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@447994 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/common/utils')
-rw-r--r-- | cpp/common/utils/inc/logger.h | 82 | ||||
-rw-r--r-- | cpp/common/utils/inc/memory.h | 17 | ||||
-rw-r--r-- | cpp/common/utils/src/Makefile | 40 | ||||
-rw-r--r-- | cpp/common/utils/src/logger.cpp | 209 | ||||
-rw-r--r-- | cpp/common/utils/src/logger_test.cpp | 78 |
5 files changed, 426 insertions, 0 deletions
diff --git a/cpp/common/utils/inc/logger.h b/cpp/common/utils/inc/logger.h new file mode 100644 index 0000000000..8a57854476 --- /dev/null +++ b/cpp/common/utils/inc/logger.h @@ -0,0 +1,82 @@ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed 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. + * + */ +/********************************************************************* +* +* NOTE: This is a lightweight logging class intended for debugging and +* verification purposes only. +* +* DO NOT USE FOR PRODUCT DEVELOPMENT - Rather, use an agreed upon +* established logging class (such as Apache's log4cxx) for product +* development purposes. +* +*********************************************************************/ + +#ifndef __LOGGER__ +#define __LOGGER__ + +#include <fstream> +#include <iostream> + +namespace qpid { +namespace utils { + +class Logger : public std::ofstream +{ + private: + bool echo_flag; + bool timestamp_flag; + bool eol_flag; + char buff[128]; // Buffer for writing formatted strings + + void write_timestamp(); + + public: + Logger(const char* filename, const bool append); + Logger(std::string& filename, const bool append); + ~Logger(); + + bool getEchoFlag() {return echo_flag;} + bool setEchoFlag(const bool _echo_flag) {echo_flag = _echo_flag;} + bool getTimestampFlag() {return timestamp_flag;} + bool setTimestampFlag(const bool _timestamp_flag) {timestamp_flag = _timestamp_flag;} + + void log(const char* message); + void log(const char* message, const bool echo); + void log(const char* message, const bool echo, const bool timestamp); + + Logger& operator<< (bool b); + Logger& operator<< (const short s); + Logger& operator<< (const unsigned short us); + Logger& operator<< (const int i); + Logger& operator<< (const unsigned int ui); + Logger& operator<< (const long l); + Logger& operator<< (const unsigned long ul); + Logger& operator<< (const long long l); + Logger& operator<< (const unsigned long long ul); + Logger& operator<< (const float f); + Logger& operator<< (const double d); + Logger& operator<< (const long double ld); + Logger& operator<< (const char* cstr); + Logger& operator<< (const std::string& str); +}; + +} +} + + +#endif diff --git a/cpp/common/utils/inc/memory.h b/cpp/common/utils/inc/memory.h new file mode 100644 index 0000000000..2d65877adb --- /dev/null +++ b/cpp/common/utils/inc/memory.h @@ -0,0 +1,17 @@ +#ifndef __UTIL_MEMORY__ +#define __UTIL_MEMORY__ + +#if __GNUC__ < 4 + #include "boost/shared_ptr.hpp" + namespace std { + namespace tr1 { + using boost::shared_ptr; + using boost::dynamic_pointer_cast; + using boost::static_pointer_cast; + } + } +#else + #include <tr1/memory> +#endif +#endif + diff --git a/cpp/common/utils/src/Makefile b/cpp/common/utils/src/Makefile new file mode 100644 index 0000000000..0185ab9975 --- /dev/null +++ b/cpp/common/utils/src/Makefile @@ -0,0 +1,40 @@ + # + # Copyright (c) 2006 The Apache Software Foundation + # + # Licensed 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. + # +##### Options ##### +QPID_HOME = ../../../.. + +include ${QPID_HOME}/cpp/options.mk + +##### Compiler flags ##### +CXXFLAGS = -I ../inc -I ${APR_HOME}/include/apr-1/ + +##### Targets ##### +# Add additional source files to SOURCE LIST to include them in the build. +COMMON_SOURCE_LIST = logger.cpp + +COMMON_OBJ_LIST = $(COMMON_SOURCE_LIST:.cpp=.o) +LOGGER_TEST_EXE = logger_test + + +.PHONY: all clean + +all: $(LOGGER_TEST_EXE) + +$(LOGGER_TEST_EXE) : $(COMMON_OBJ_LIST) $(LOGGER_TEST_EXE).o + $(CXX) -o $@ $^ -l apr-1 -L /usr/local/apr/lib/ + +clean: + -@rm -f $(LOGGER_TEST_EXE) $(LOGGER_TEST_EXE).o $(COMMON_OBJ_LIST) test_log.txt *~ ../inc/*~ diff --git a/cpp/common/utils/src/logger.cpp b/cpp/common/utils/src/logger.cpp new file mode 100644 index 0000000000..603fa6574e --- /dev/null +++ b/cpp/common/utils/src/logger.cpp @@ -0,0 +1,209 @@ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed 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. + * + */ +/********************************************************************* +* +* NOTE: This is a lightweight logging class intended for debugging and +* verification purposes only. +* +* DO NOT USE FOR PRODUCT DEVELOPMENT - Rather, use an agreed upon +* established logging class (such as Apache's log4cxx) for product +* development purposes. +* +*********************************************************************/ + +#include <iostream> +#include <ostream> +#include <string.h> +#include "apr_time.h" +#include "logger.h" + +namespace qpid { +namespace utils { + +Logger::Logger(const char* filename, const bool append): + std::ofstream(filename, append ? std::ios::app : std::ios::out) +{ + echo_flag = false; + timestamp_flag = true; + eol_flag = true; +} + +Logger::Logger(std::string& filename, const bool append): + std::ofstream(filename.c_str(), append ? std::ios::app : std::ios::out) +{ + echo_flag = false; + timestamp_flag = true; + eol_flag = true; +} + +Logger::~Logger() +{ + close(); +} + +void Logger::write_timestamp() +{ + int len; + apr_time_exp_t now; + apr_time_exp_lt(&now, apr_time_now()); + sprintf(buff, "%4d/%02d/%02d %02d:%02d:%02d.%06d : ", 1900+now.tm_year, now.tm_mon, + now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, now.tm_usec); + write(buff, strlen(buff)); +} + + +void Logger::log(const char* message) +{ + if (timestamp_flag && eol_flag) + { + eol_flag = false; + write_timestamp(); + } + write(message, strlen(message)); + if (echo_flag) + std::cout << message; + if (strchr(message, '\n')) + eol_flag = true; +} + +void Logger::log(const char* message, const bool echo) +{ + if (timestamp_flag && eol_flag) + { + eol_flag = false; + write_timestamp(); + } + write(message, strlen(message)); + if (echo) + std::cout << message; + if (strchr(message, '\n')) + eol_flag = true; +} + +void Logger::log(const char* message, const bool echo, const bool timestamp) +{ + if (timestamp && eol_flag) + { + eol_flag = false; + write_timestamp(); + } + write(message, strlen(message)); + if (echo) + std::cout << message; + if (strchr(message, '\n')) + eol_flag = true; +} + +Logger& Logger::operator<< (const bool b) +{ + log(b ? "true" : "false"); + return *this; +} + +Logger& Logger::operator<< (const short s) +{ + sprintf(buff, "%d", s); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const unsigned short us) +{ + sprintf(buff, "%u", us); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const int i) +{ + sprintf(buff, "%d", i); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const unsigned int ui) +{ + sprintf(buff, "%u", ui); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const long l) +{ + sprintf(buff, "%ld", l); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const unsigned long ul) +{ + sprintf(buff, "%lu", ul); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const long long l) +{ + sprintf(buff, "%ld", l); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const unsigned long long ul) +{ + sprintf(buff, "%lu", ul); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const float f) +{ + sprintf(buff, "%f", f); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const double d) +{ + sprintf(buff, "%lf", d); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const long double ld) +{ + sprintf(buff, "%Lf", ld); + log(buff); + return *this; +} + +Logger& Logger::operator<< (const char* cstr) +{ + log(cstr); + return *this; +} + +Logger& Logger::operator<< (const std::string& str) +{ + log(str.c_str()); + return *this; +} + +} +} + diff --git a/cpp/common/utils/src/logger_test.cpp b/cpp/common/utils/src/logger_test.cpp new file mode 100644 index 0000000000..1866af9fbb --- /dev/null +++ b/cpp/common/utils/src/logger_test.cpp @@ -0,0 +1,78 @@ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed 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 <iostream> +#include <string> +#include "logger.h" + +using namespace qpid::utils; + +void run_sequence(Logger& log) +{ + bool b = true; + short s = -5; + unsigned short us = 12; + int i = -2345; + unsigned int ui = 34567; + long l = -12345678; + unsigned long ul = 23456789; + long long ll = -1234567890; + unsigned long long ull = 1234567890; + float f = -123.45678; + double d = 123.45678901; + long double ld = 23456.789012345678; + char* cstr = "This is a test C string."; + char* cr = "\n"; + std::string str("This is a test std::string"); + log << "bool = " << b << cr; + log << "short = " << s << cr; + log << "unsigned sort = " << us << cr; + log << "int = " << i << cr; + log << "unsigned int = " << ui << cr; + log << "long = " << l << cr; + log << "unsigned long = " << ul << cr; + log << "long long = " << ll << cr; + log << "unsigned long long = " << ull << cr; + log << "float = " << f << cr; + log << "double = " << d << cr; + log << "long double = " << ld << cr; + log << "char* = " << cstr << cr; + log << "std::string = " << str << cr; + log << "String 1\n"; + log << "String 2\n" << "String 3 " << "String 4\n"; + log << "Literal bool = " << false << cr; + log << "Literal unsigned int = " << 15 << cr; + log << "Literal double = " << (double)15 << cr; +} + +int main(int argc, char** argv) +{ + Logger log("test_log.txt", false); + std::cout << "****** Initial state (echo off, timestamp on)" << std::endl; + run_sequence(log); + std::cout << std::endl << "****** (echo off, timestamp off)" << std::endl; + log.setTimestampFlag(false); + run_sequence(log); + std::cout << std::endl << "****** (echo on, timestamp on)" << std::endl; + log.setEchoFlag(true); + log.setTimestampFlag(true); + run_sequence(log); + std::cout << std::endl << "****** (echo on, timestamp off)" << std::endl; + log.setTimestampFlag(false); + run_sequence(log); + return 0; +} |