Cosmetic: Coding style fixes.

This commit is contained in:
Felipe Zimmerle 2016-10-24 10:04:27 -03:00
parent 8757840bc3
commit 730d7dbd28
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
3 changed files with 30 additions and 27 deletions

View File

@ -29,7 +29,8 @@ DebugLog::~DebugLog() {
DebugLogWriter::getInstance().close(m_fileName); DebugLogWriter::getInstance().close(m_fileName);
} }
void DebugLog::setDebugLogFile(const std::string& fileName, std::string *error) { void DebugLog::setDebugLogFile(const std::string& fileName,
std::string *error) {
if (isLogFileSet()) { if (isLogFileSet()) {
DebugLogWriter::getInstance().close(m_fileName); DebugLogWriter::getInstance().close(m_fileName);
} }

View File

@ -17,9 +17,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fstream>
#include <pthread.h> #include <pthread.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
@ -27,6 +25,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fstream>
namespace modsecurity { namespace modsecurity {
@ -37,7 +37,7 @@ debug_log_file_handler_t *DebugLogWriter::find_handler(
if (current->file_name == fileName) { if (current->file_name == fileName) {
return current; return current;
} }
current = (debug_log_file_handler_t *) current->next; current = reinterpret_cast<debug_log_file_handler_t *>(current->next);
} }
return NULL; return NULL;
@ -55,7 +55,7 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler(
FILE *fp; FILE *fp;
fp = fopen(fileName.c_str(), "a"); fp = fopen(fileName.c_str(), "a");
if (fp <= 0) { if (fp == 0) {
error->assign("Failed to open file: " + fileName); error->assign("Failed to open file: " + fileName);
goto err_fh; goto err_fh;
} }
@ -64,14 +64,14 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler(
if (mem_key_structure < 0) { if (mem_key_structure < 0) {
error->assign("Failed to select key for the shared memory (1): "); error->assign("Failed to select key for the shared memory (1): ");
error->append(strerror(errno)); error->append(strerror(errno));
goto err_mem_key1; goto err_mem_key;
} }
mem_key_file_name = ftok(fileName.c_str(), 2); mem_key_file_name = ftok(fileName.c_str(), 2);
if (mem_key_file_name < 0) { if (mem_key_file_name < 0) {
error->assign("Failed to select key for the shared memory (2): "); error->assign("Failed to select key for the shared memory (2): ");
error->append(strerror(errno)); error->append(strerror(errno));
goto err_mem_key1; goto err_mem_key;
} }
shm_id = shmget(mem_key_structure, sizeof (debug_log_file_handler_t), shm_id = shmget(mem_key_structure, sizeof (debug_log_file_handler_t),
@ -82,8 +82,9 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler(
goto err_shmget1; goto err_shmget1;
} }
new_debug_log = (debug_log_file_handler_t *) shmat(shm_id, NULL, 0); new_debug_log = reinterpret_cast<debug_log_file_handler_t *>(
if (((char *)new_debug_log)[0] == -1) { shmat(shm_id, NULL, 0));
if ((reinterpret_cast<char *>(new_debug_log)[0]) == -1) {
error->assign("Failed to attach shared memory (1): "); error->assign("Failed to attach shared memory (1): ");
error->append(strerror(errno)); error->append(strerror(errno));
goto err_shmat1; goto err_shmat1;
@ -104,14 +105,14 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler(
goto err_shmget2; goto err_shmget2;
} }
new_debug_log->shm_id_file_name = shm_id; new_debug_log->shm_id_file_name = shm_id;
shm_ptr2 = (char *) shmat(shm_id, NULL, 0); shm_ptr2 = reinterpret_cast<char *>(shmat(shm_id, NULL, 0));
if (shm_ptr2[0] == -1) { if (shm_ptr2[0] == -1) {
error->assign("Failed to attach shared memory (2): "); error->assign("Failed to attach shared memory (2): ");
error->append(strerror(errno)); error->append(strerror(errno));
goto err_shmat2; goto err_shmat2;
} }
memset(shm_ptr2, '\0', sizeof (debug_log_file_handler_t));
memcpy(shm_ptr2, fileName.c_str(), fileName.size()); memcpy(shm_ptr2, fileName.c_str(), fileName.size());
shm_ptr2[fileName.size()] = '\0';
new_debug_log->file_name = shm_ptr2; new_debug_log->file_name = shm_ptr2;
@ -126,7 +127,8 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler(
new_debug_log->next = NULL; new_debug_log->next = NULL;
current = NULL; current = NULL;
} else { } else {
current = (debug_log_file_handler_t *) current->next; current = reinterpret_cast<debug_log_file_handler_t *>(
current->next);
} }
} }
} }
@ -139,8 +141,7 @@ err_shmat2:
err_shmget1: err_shmget1:
err_shmat1: err_shmat1:
shmdt(new_debug_log); shmdt(new_debug_log);
err_mem_key2: err_mem_key:
err_mem_key1:
err_fh: err_fh:
return NULL; return NULL;
} }
@ -167,7 +168,6 @@ int DebugLogWriter::open(const std::string& fileName, std::string *error) {
void DebugLogWriter::close(const std::string& fileName) { void DebugLogWriter::close(const std::string& fileName) {
debug_log_file_handler_t *a; debug_log_file_handler_t *a;
bool first = false;
if (fileName.empty()) { if (fileName.empty()) {
return; return;
@ -181,19 +181,20 @@ void DebugLogWriter::close(const std::string& fileName) {
a->using_it--; a->using_it--;
if (a->using_it == 0) { if (a->using_it == 0) {
bool first = false;
int shm_id1 = a->shm_id_structure; int shm_id1 = a->shm_id_structure;
int shm_id2 = a->shm_id_file_name; int shm_id2 = a->shm_id_file_name;
debug_log_file_handler_t *p , *n; debug_log_file_handler_t *p , *n;
pthread_mutex_lock(&a->lock); pthread_mutex_lock(&a->lock);
fclose(a->fp); fclose(a->fp);
p = (debug_log_file_handler_t *) a->previous; p = reinterpret_cast<debug_log_file_handler_t *>(a->previous);
n = (debug_log_file_handler_t *) a->next; n = reinterpret_cast<debug_log_file_handler_t *>(a->next);
if (p != NULL) { if (p != NULL) {
p->next = (debug_log_file_handler_t *) n; p->next = reinterpret_cast<debug_log_file_handler_t *>(n);
} }
if (n != NULL) { if (n != NULL) {
n->previous = (debug_log_file_handler_t *) p; n->previous = reinterpret_cast<debug_log_file_handler_t *>(p);
} }
a->previous = NULL; a->previous = NULL;
a->next = NULL; a->next = NULL;
@ -211,7 +212,7 @@ void DebugLogWriter::close(const std::string& fileName) {
shmctl(shm_id2, IPC_RMID, NULL); shmctl(shm_id2, IPC_RMID, NULL);
if (first) { if (first) {
m_first == NULL; m_first = NULL;
} }
a = NULL; a = NULL;
} }
@ -230,7 +231,8 @@ void DebugLogWriter::write_log(const std::string& fileName,
} }
pthread_mutex_lock(&a->lock); pthread_mutex_lock(&a->lock);
wrote = write(a->file_handler, (void *)lmsg.c_str(), lmsg.size()); wrote = write(a->file_handler, reinterpret_cast<const char *>(lmsg.c_str()),
lmsg.size());
if (wrote < msg.size()) { if (wrote < msg.size()) {
std::cerr << "failed to write debug log: " << msg; std::cerr << "failed to write debug log: " << msg;
} }

View File

@ -13,17 +13,17 @@
* *
*/ */
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
#include <stdio.h> #include <stdio.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/types.h> #include <sys/types.h>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#ifndef SRC_DEBUG_LOG_WRITER_H_ #ifndef SRC_DEBUG_LOG_WRITER_H_
#define SRC_DEBUG_LOG_WRITER_H_ #define SRC_DEBUG_LOG_WRITER_H_