code style
This commit is contained in:
@@ -27,8 +27,7 @@ static bool clockInitialized = false;
|
||||
static clock_serv_t clockServ;
|
||||
#endif
|
||||
|
||||
void clockGettime(struct timespec* t)
|
||||
{
|
||||
void clockGettime(struct timespec* t) {
|
||||
#ifdef __MACH__
|
||||
if (!clockInitialized) {
|
||||
clockInitialized = true;
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_CLOCK_H_
|
||||
#define LIBUTILS_CLOCK_H_
|
||||
#ifndef LIB_UTILS_CLOCK_H_
|
||||
#define LIB_UTILS_CLOCK_H_
|
||||
|
||||
#include <time.h>
|
||||
|
||||
@@ -29,4 +29,4 @@
|
||||
*/
|
||||
void clockGettime(struct timespec* t);
|
||||
|
||||
#endif // LIBUTILS_CLOCK_H_
|
||||
#endif // LIB_UTILS_CLOCK_H_
|
||||
|
||||
+14
-24
@@ -57,8 +57,7 @@ static LogLevel s_logLevel = ll_notice;
|
||||
/** the current log FILE. */
|
||||
static FILE* s_logFile = stdout;
|
||||
|
||||
bool setLogFacilities(const char* facilities)
|
||||
{
|
||||
bool setLogFacilities(const char* facilities) {
|
||||
char *input = strdup(facilities);
|
||||
char *opt = (char*)input, *value = NULL;
|
||||
int newFacilites = 0;
|
||||
@@ -80,14 +79,13 @@ bool setLogFacilities(const char* facilities)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool getLogFacilities(char* buffer)
|
||||
{
|
||||
if (s_logFacilites==LF_ALL) {
|
||||
bool getLogFacilities(char* buffer) {
|
||||
if (s_logFacilites == LF_ALL) {
|
||||
return strcpy(buffer, facilityNames[lf_COUNT]) != NULL;
|
||||
}
|
||||
*buffer = 0; // for strcat to work
|
||||
bool found = false;
|
||||
for (int val=0; val<lf_COUNT; val++) {
|
||||
for (int val = 0; val < lf_COUNT; val++) {
|
||||
if (s_logFacilites&(1<<val)) {
|
||||
if (found) {
|
||||
strcat(buffer, ",");
|
||||
@@ -99,8 +97,7 @@ bool getLogFacilities(char* buffer)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool setLogLevel(const char* level)
|
||||
{
|
||||
bool setLogLevel(const char* level) {
|
||||
char *input = strdup(level);
|
||||
char *opt = (char*)input, *value = NULL;
|
||||
int newLevel = 0;
|
||||
@@ -117,24 +114,21 @@ bool setLogLevel(const char* level)
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* getLogLevel()
|
||||
{
|
||||
const char* getLogLevel() {
|
||||
return levelNames[s_logLevel];
|
||||
}
|
||||
|
||||
bool setLogFile(const char* filename)
|
||||
{
|
||||
bool setLogFile(const char* filename) {
|
||||
FILE* newFile = fopen(filename, "a");
|
||||
if (newFile == NULL)
|
||||
if (newFile == NULL) {
|
||||
return false;
|
||||
|
||||
}
|
||||
closeLogFile();
|
||||
s_logFile = newFile;
|
||||
return true;
|
||||
}
|
||||
|
||||
void closeLogFile()
|
||||
{
|
||||
void closeLogFile() {
|
||||
if (s_logFile != NULL) {
|
||||
if (s_logFile != stdout) {
|
||||
fclose(s_logFile);
|
||||
@@ -143,14 +137,12 @@ void closeLogFile()
|
||||
}
|
||||
}
|
||||
|
||||
bool needsLog(const LogFacility facility, const LogLevel level)
|
||||
{
|
||||
bool needsLog(const LogFacility facility, const LogLevel level) {
|
||||
return ((s_logFacilites & (1<<facility)) != 0)
|
||||
&& (s_logLevel >= level);
|
||||
}
|
||||
|
||||
void logWrite(const char* facility, const char* level, const char* message, va_list ap)
|
||||
{
|
||||
void logWrite(const char* facility, const char* level, const char* message, va_list ap) {
|
||||
struct timespec ts;
|
||||
struct tm* tm;
|
||||
clockGettime(&ts);
|
||||
@@ -168,16 +160,14 @@ void logWrite(const char* facility, const char* level, const char* message, va_l
|
||||
}
|
||||
}
|
||||
|
||||
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...)
|
||||
{
|
||||
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facilityNames[facility], levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void logWrite(const char* facility, const LogLevel level, const char* message, ...)
|
||||
{
|
||||
void logWrite(const char* facility, const LogLevel level, const char* message, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facility, levelNames[level], message, ap);
|
||||
|
||||
+15
-15
@@ -16,19 +16,19 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_LOG_H_
|
||||
#define LIBUTILS_LOG_H_
|
||||
#ifndef LIB_UTILS_LOG_H_
|
||||
#define LIB_UTILS_LOG_H_
|
||||
|
||||
/** \file log.h */
|
||||
|
||||
/** the available log facilities. */
|
||||
enum LogFacility {
|
||||
lf_main=0, //!< main loop
|
||||
lf_network, //!< network related
|
||||
lf_bus, //!< eBUS related
|
||||
lf_update, //!< updates found while listening to the bus
|
||||
lf_other, //!< all other log facilities
|
||||
lf_COUNT=5 //!< number of available log facilities
|
||||
lf_main = 0, //!< main loop
|
||||
lf_network, //!< network related
|
||||
lf_bus, //!< eBUS related
|
||||
lf_update, //!< updates found while listening to the bus
|
||||
lf_other, //!< all other log facilities
|
||||
lf_COUNT = 5 //!< number of available log facilities
|
||||
};
|
||||
|
||||
/** macro for enabling all log facilities. */
|
||||
@@ -36,12 +36,12 @@ enum LogFacility {
|
||||
|
||||
/** the available log levels. */
|
||||
enum LogLevel {
|
||||
ll_none=0, //!< no level at all
|
||||
ll_error, //!< error message
|
||||
ll_notice, //!< important message
|
||||
ll_info, //!< informational message
|
||||
ll_debug, //!< debugging message (normally suppressed)
|
||||
ll_COUNT=5 //!< number of available log levels
|
||||
ll_none = 0, //!< no level at all
|
||||
ll_error, //!< error message
|
||||
ll_notice, //!< important message
|
||||
ll_info, //!< informational message
|
||||
ll_debug, //!< debugging message (normally suppressed)
|
||||
ll_COUNT = 5 //!< number of available log levels
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -136,4 +136,4 @@ void logWrite(const char* facility, const LogLevel level, const char* message, .
|
||||
/** A macro for a debug message that calls the logging function only if needed. */
|
||||
#define logOtherDebug(facility, ...) (needsLog(lf_other, ll_debug) ? logWrite(facility, ll_debug, __VA_ARGS__) : void(0))
|
||||
|
||||
#endif // LIBUTILS_LOG_H_
|
||||
#endif // LIB_UTILS_LOG_H_
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_NOTIFY_H_
|
||||
#define LIBUTILS_NOTIFY_H_
|
||||
#ifndef LIB_UTILS_NOTIFY_H_
|
||||
#define LIB_UTILS_NOTIFY_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
@@ -73,6 +73,6 @@ private:
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_NOTIFY_H_
|
||||
#endif // LIB_UTILS_NOTIFY_H_
|
||||
|
||||
|
||||
|
||||
+14
-12
@@ -16,12 +16,12 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_QUEUE_H_
|
||||
#define LIBUTILS_QUEUE_H_
|
||||
#ifndef LIB_UTILS_QUEUE_H_
|
||||
#define LIB_UTILS_QUEUE_H_
|
||||
|
||||
#include <list>
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
#include <list>
|
||||
#include "clock.h"
|
||||
|
||||
/** \file queue.h */
|
||||
@@ -82,22 +82,23 @@ public:
|
||||
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
|
||||
* @return the item, or NULL if no item is available within the specified time.
|
||||
*/
|
||||
T pop(int timeout=0)
|
||||
T pop(int timeout = 0)
|
||||
{
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (timeout>0) {
|
||||
if (timeout > 0) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += timeout;
|
||||
while (m_queue.empty()) {
|
||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t)==ETIMEDOUT)
|
||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) == ETIMEDOUT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_queue.empty())
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
else {
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
m_queue.pop_front();
|
||||
}
|
||||
@@ -111,7 +112,7 @@ public:
|
||||
* @param wait true to wait for the item to appear in the queue.
|
||||
* @return whether the item was removed.
|
||||
*/
|
||||
bool remove(T item, bool wait=false)
|
||||
bool remove(T item, bool wait = false)
|
||||
{
|
||||
bool ret = false;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
@@ -138,10 +139,11 @@ public:
|
||||
{
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_queue.empty())
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
else
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
@@ -158,4 +160,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_QUEUE_H_
|
||||
#endif // LIB_UTILS_QUEUE_H_
|
||||
|
||||
@@ -17,28 +17,27 @@
|
||||
*/
|
||||
|
||||
#include "rotatefile.h"
|
||||
#include "clock.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/file.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "clock.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
RotateFile::~RotateFile()
|
||||
{
|
||||
RotateFile::~RotateFile() {
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool RotateFile::setEnabled(bool enabled)
|
||||
{
|
||||
if (enabled==m_enabled) {
|
||||
bool RotateFile::setEnabled(bool enabled) {
|
||||
if (enabled == m_enabled) {
|
||||
return false;
|
||||
}
|
||||
m_enabled = enabled;
|
||||
@@ -53,8 +52,7 @@ bool RotateFile::setEnabled(bool enabled)
|
||||
return true;
|
||||
}
|
||||
|
||||
void RotateFile::write(unsigned char* value, unsigned int size, bool received)
|
||||
{
|
||||
void RotateFile::write(unsigned char* value, unsigned int size, bool received) {
|
||||
if (!m_enabled || !m_stream) {
|
||||
return;
|
||||
}
|
||||
@@ -68,7 +66,7 @@ void RotateFile::write(unsigned char* value, unsigned int size, bool received)
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
|
||||
received ? '<' : '>'
|
||||
);
|
||||
for (unsigned int pos=0; pos<size; pos++) {
|
||||
for (unsigned int pos = 0; pos < size; pos++) {
|
||||
fprintf(m_stream, "%2.2x ", value[pos]);
|
||||
}
|
||||
fprintf(m_stream, "\n");
|
||||
|
||||
@@ -16,12 +16,13 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_ROTATEFILE_H_
|
||||
#define LIBUTILS_ROTATEFILE_H_
|
||||
#ifndef LIB_UTILS_ROTATEFILE_H_
|
||||
#define LIB_UTILS_ROTATEFILE_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
/** @file rotatefile.h
|
||||
* Helpers for writing to rotating files.
|
||||
@@ -41,7 +42,7 @@ public:
|
||||
* @param maxSize the maximum size of the file to write to.
|
||||
* @param textMode whether to write each byte with prefixed timestamp and direction as text.
|
||||
*/
|
||||
RotateFile(const string fileName, const unsigned long maxSize, const bool textMode=false)
|
||||
RotateFile(const string fileName, const unsigned long maxSize, const bool textMode = false)
|
||||
: m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {}
|
||||
|
||||
/**
|
||||
@@ -54,7 +55,7 @@ public:
|
||||
* @param enabled @p true to enable writing to the file, @p false to disable it.
|
||||
* @return @p true when the state was changed, @p false otherwise.
|
||||
*/
|
||||
bool setEnabled(bool enabled=true);
|
||||
bool setEnabled(bool enabled = true);
|
||||
|
||||
/**
|
||||
* Return whether writing to the file is enabled.
|
||||
@@ -68,7 +69,7 @@ public:
|
||||
* @param size the number of bytes to write.
|
||||
* @param received @a true on reception, @a false on sending (only relevant in text mode).
|
||||
*/
|
||||
void write(unsigned char* value, unsigned int size, bool received=true);
|
||||
void write(unsigned char* value, unsigned int size, bool received = true);
|
||||
|
||||
private:
|
||||
/** whether writing to the file is enabled. */
|
||||
@@ -91,5 +92,5 @@ private:
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_ROTATEFILE_H_
|
||||
#endif // LIB_UTILS_ROTATEFILE_H_
|
||||
|
||||
|
||||
@@ -17,30 +17,27 @@
|
||||
*/
|
||||
|
||||
#include "tcpsocket.h"
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd)
|
||||
{
|
||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd) {
|
||||
char ip[17];
|
||||
inet_ntop(AF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, (socklen_t)sizeof(ip)-1);
|
||||
m_ip = ip;
|
||||
m_port = (uint16_t)ntohs(address->sin_port);
|
||||
}
|
||||
|
||||
bool TCPSocket::isValid()
|
||||
{
|
||||
bool TCPSocket::isValid() {
|
||||
return fcntl(m_sfd, F_GETFL) != -1;
|
||||
}
|
||||
|
||||
|
||||
TCPSocket* TCPClient::connect(const string& server, const uint16_t& port)
|
||||
{
|
||||
TCPSocket* TCPClient::connect(const string& server, const uint16_t& port) {
|
||||
struct sockaddr_in address;
|
||||
int ret;
|
||||
|
||||
@@ -76,8 +73,7 @@ TCPSocket* TCPClient::connect(const string& server, const uint16_t& port)
|
||||
}
|
||||
|
||||
|
||||
int TCPServer::start()
|
||||
{
|
||||
int TCPServer::start() {
|
||||
if (m_listening) {
|
||||
return 0;
|
||||
}
|
||||
@@ -109,8 +105,7 @@ int TCPServer::start()
|
||||
return result;
|
||||
}
|
||||
|
||||
TCPSocket* TCPServer::newSocket()
|
||||
{
|
||||
TCPSocket* TCPServer::newSocket() {
|
||||
if (!m_listening) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -16,13 +16,13 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_TCPSOCKET_H_
|
||||
#define LIBUTILS_TCPSOCKET_H_
|
||||
#ifndef LIB_UTILS_TCPSOCKET_H_
|
||||
#define LIB_UTILS_TCPSOCKET_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
/** \file tcpsocket.h */
|
||||
|
||||
@@ -181,5 +181,5 @@ private:
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_TCPSOCKET_H_
|
||||
#endif // LIB_UTILS_TCPSOCKET_H_
|
||||
|
||||
|
||||
@@ -23,22 +23,19 @@
|
||||
#include "thread.h"
|
||||
#include "clock.h"
|
||||
|
||||
void* Thread::runThread(void* arg)
|
||||
{
|
||||
void* Thread::runThread(void* arg) {
|
||||
((Thread*)arg)->enter();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Thread::~Thread()
|
||||
{
|
||||
Thread::~Thread() {
|
||||
if (m_started) {
|
||||
pthread_cancel(m_threadid);
|
||||
pthread_detach(m_threadid);
|
||||
}
|
||||
}
|
||||
|
||||
bool Thread::start(const char* name)
|
||||
{
|
||||
bool Thread::start(const char* name) {
|
||||
|
||||
int result = pthread_create(&m_threadid, NULL, runThread, this);
|
||||
|
||||
@@ -58,8 +55,7 @@ bool Thread::start(const char* name)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Thread::join()
|
||||
{
|
||||
bool Thread::join() {
|
||||
int result = -1;
|
||||
|
||||
if (m_started) {
|
||||
@@ -82,36 +78,31 @@ void Thread::enter() {
|
||||
|
||||
|
||||
WaitThread::WaitThread()
|
||||
: Thread()
|
||||
{
|
||||
: Thread() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
|
||||
WaitThread::~WaitThread()
|
||||
{
|
||||
WaitThread::~WaitThread() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
|
||||
void WaitThread::stop()
|
||||
{
|
||||
void WaitThread::stop() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
Thread::stop();
|
||||
}
|
||||
|
||||
bool WaitThread::join()
|
||||
{
|
||||
bool WaitThread::join() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return Thread::join();
|
||||
}
|
||||
|
||||
bool WaitThread::Wait(int seconds)
|
||||
{
|
||||
bool WaitThread::Wait(int seconds) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += seconds;
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_THREAD_H_
|
||||
#define LIBUTILS_THREAD_H_
|
||||
#ifndef LIB_UTILS_THREAD_H_
|
||||
#define LIB_UTILS_THREAD_H_
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
@@ -145,4 +145,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_THREAD_H_
|
||||
#endif // LIB_UTILS_THREAD_H_
|
||||
|
||||
Reference in New Issue
Block a user