minor code cleanup

This commit is contained in:
mpc
2004-07-17 04:54:45 +00:00
committed by zzz
parent 392cbb817e
commit 5f022e6e1f
9 changed files with 97 additions and 99 deletions

View File

@@ -26,13 +26,11 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#include <cstdarg>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#include "platform.hpp"
#include "mutex.hpp"
#include "time.hpp"
#include "logger.hpp"
@@ -41,10 +39,10 @@ using namespace Libsockthread;
/*
* Closes the log file
*/
void Logger::close(void)
void Logger::close()
{
logf_m.lock();
if (logf == 0) {
if (logf == NULL) {
logf_m.unlock();
return;
}
@@ -53,7 +51,7 @@ void Logger::close(void)
cerr << "fclose() failed: " << strerror(errno) << '\n';
cerr_m.unlock();
}
logf = 0;
logf = NULL;
logf_m.unlock();
}
@@ -88,15 +86,14 @@ void Logger::log(priority_t priority, const char* format, ...)
va_list ap;
va_start(ap, format);
string s;
Time t;
logf_m.lock();
if (logf != 0) {
if (logf != NULL) {
/*
* Remember! If you change the format here, change it in the else too
*/
fprintf(logf, "%c %s ", ll, t.utc(s).c_str());
fprintf(logf, "%c %s ", ll, t.utc().c_str());
vfprintf(logf, format, ap);
fputc('\n', logf);
if (fflush(logf) == EOF) {
@@ -106,7 +103,7 @@ void Logger::log(priority_t priority, const char* format, ...)
}
} else {
// if they don't have an open log file, just use stderr
fprintf(stderr, "%c %s ", ll, t.utc(s).c_str());
fprintf(stderr, "%c %s ", ll, t.utc().c_str());
vfprintf(stderr, format, ap);
fputc('\n', stderr);
}
@@ -118,8 +115,8 @@ void Logger::log(priority_t priority, const char* format, ...)
}
/*
* Opens a log file for appending. If there already is an open log file, then
* it is closed and the new one is opened.
* Opens a log file for appending. If a log file is already open, then it is
* closed and the new one is opened.
*
* file - file location to open
*/
@@ -145,8 +142,8 @@ bool Logger::open(const string& file)
// g++ -Wall -c mutex.cpp -o mutex.o
// g++ -Wall -c time.cpp -o time.o
// g++ -Wall -DUNIT_TEST -c logger.cpp -o logger.o
// g++ -Wall -DUNIT_TEST logger.o mutex.o thread.o time.o -o logger -lpthread
int main(void)
// g++ -Wall -DUNIT_TEST logger.o mutex.o thread.o time.o -o logger -pthread
int main()
{
Logger logger;

View File

@@ -26,6 +26,8 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#ifndef LIBSOCKTHREAD_LOGGER_HPP
@@ -40,8 +42,7 @@
* LWARN - errors we automatically recover from
* LERROR - major, important errors
*
* Obviously, these only work if your Logger object is called "logger" and is
* global
* These only work if your Logger object is called "logger"
*/
// Prints out the file name, function name, and line number before the message
#define LDEBUG(format, ...) logger.log(Logger::DEBUG, "%s:%s:%d:" \
@@ -69,16 +70,17 @@
namespace Libsockthread {
class Logger {
public:
typedef enum {DEBUG = 0, MINOR = 1, INFO = 2, WARN = 3, ERROR = 4}
priority_t;
enum priority_t {DEBUG = 0, MINOR = 1, INFO = 2, WARN = 3,
ERROR = 4};
Logger(void)
: logf(0), loglevel(Logger::DEBUG) { }
~Logger(void) { close(); }
Logger()
: logf(NULL), loglevel(Logger::DEBUG) { }
~Logger()
{ close(); }
void close(void);
void close();
void log(priority_t priority, const char* format, ...);
priority_t get_loglevel(void)
priority_t get_loglevel()
{ loglevel_m.lock(); priority_t ll = loglevel;
loglevel_m.unlock(); return ll; }
bool open(const string& file);

View File

@@ -26,31 +26,28 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
// Modelled after JThread by Jori Liesenborgs
/*
* Modelled after JThread by Jori Liesenborgs
*/
#include <cassert>
#include "platform.hpp"
#ifdef WINTHREAD
#include <windows.h>
#else
#include <pthread.h>
#endif
using namespace std;
#include "mutex.hpp"
using namespace Libsockthread;
/*
* Creates a mutex
*/
Mutex::Mutex(void)
Mutex::Mutex()
{
#ifdef WINTHREAD
mutex = CreateMutex(0, false, 0);
assert(mutex != 0);
mutex = CreateMutex(NULL, false, NULL);
assert(mutex != NULL);
#else
int rc = pthread_mutex_init(&mutex, 0);
int rc = pthread_mutex_init(&mutex, NULL);
assert(rc == 0);
#endif
}
@@ -58,7 +55,7 @@ Mutex::Mutex(void)
/*
* Destroys a mutex
*/
Mutex::~Mutex(void)
Mutex::~Mutex()
{
#ifdef WINTHREAD
BOOL rc = CloseHandle(mutex);
@@ -72,7 +69,7 @@ Mutex::~Mutex(void)
/*
* Locks the mutex
*/
void Mutex::lock(void)
void Mutex::lock()
{
#ifdef WINTHREAD
DWORD rc = WaitForSingleObject(mutex, INFINITE);
@@ -86,7 +83,7 @@ void Mutex::lock(void)
/*
* Unlocks the mutex
*/
void Mutex::unlock(void)
void Mutex::unlock()
{
#ifdef WINTHREAD
BOOL rc = ReleaseMutex(mutex);
@@ -100,13 +97,12 @@ void Mutex::unlock(void)
#ifdef UNIT_TEST
// g++ -Wall -c thread.cpp -o thread.o
// g++ -Wall -DUNIT_TEST -c mutex.cpp -o mutex.o
// g++ -Wall -DUNIT_TEST mutex.o thread.o -o mutex -lpthread
#include <iostream>
// g++ -Wall -DUNIT_TEST mutex.o thread.o -o mutex -pthread
#include "thread.hpp"
Mutex widget;
int main(void)
int main()
{
class Mutex_test : public Thread
{
@@ -114,7 +110,7 @@ int main(void)
Mutex_test(int n)
: testval(n) {}
void* thread(void)
void* thread()
{
widget.lock();
cout << "I got it! thread #" << testval << '\n';
@@ -122,9 +118,11 @@ int main(void)
// widget, since it is never unlocked
return 0;
}
private:
int testval;
};
Mutex_test t1(1);
Mutex_test t2(2);
Mutex_test t3(3);

View File

@@ -26,9 +26,13 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
// Modelled after JThread by Jori Liesenborgs
/*
* Modelled after JThread by Jori Liesenborgs
*/
#ifndef LIBSOCKTHREAD_MUTEX_HPP
#define LIBSOCKTHREAD_MUTEX_HPP
@@ -36,11 +40,11 @@
namespace Libsockthread {
class Mutex {
public:
Mutex(void);
~Mutex(void);
Mutex();
~Mutex();
void lock(void);
void unlock(void);
void lock();
void unlock();
private:
#ifdef WINTHREAD
HANDLE mutex;

View File

@@ -26,6 +26,8 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
/*
@@ -46,29 +48,22 @@
#define CYGWIN 3 // Cygwin
#if OS == MINGW
#define INET_ADDRSTRLEN 16
#define NO_GETHOSTBYNAME2
#define NO_INET_ATON /* implies NO_INET_PTON */
#define NO_INET_NTOP
#define WINSOCK
#define WINTHREAD
#endif
#if OS == LINUX
#define NO_GETHOSTBYNAME2
#endif
#if OS == CYGWIN
#define FAST32_IS_LONG
#define INET_ADDRSTRLEN 16
#define NO_GETHOSTBYNAME2
#define NO_INET_NTOP
#define NO_INET_PTON
#endif
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <ctime>
#include <iostream>
#ifndef WINTHREAD
#include <pthread.h>
#endif
#include <string>
#if defined WINSOCK || defined WINTHREAD
#include <windows.h>
#endif
using namespace std;
#endif // LIBSOCKTHREAD_PLATFORM_HPP

View File

@@ -26,18 +26,15 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
// Modelled after JThread by Jori Liesenborgs
/*
* Modelled after JThread by Jori Liesenborgs
*/
#include <cassert>
#include "platform.hpp"
#ifdef WINTHREAD
#include <windows.h>
#else
#include <pthread.h>
#endif
using namespace std;
#include "mutex.hpp"
#include "thread.hpp"
using namespace Libsockthread;
@@ -45,12 +42,12 @@ using namespace Libsockthread;
/*
* Gets the return value of a finished thread
*/
void* Thread::get_retval(void)
void* Thread::get_retval()
{
void* val;
running_m.lock();
if (running)
val = 0;
val = NULL;
else
val = retval;
running_m.unlock();
@@ -60,7 +57,7 @@ void* Thread::get_retval(void)
/*
* Checks whether the thread is running
*/
bool Thread::is_running(void)
bool Thread::is_running()
{
running_m.lock();
bool r = running;
@@ -72,7 +69,7 @@ bool Thread::is_running(void)
* Stops the thread
* Generally NOT a good idea
*/
void Thread::kill(void)
void Thread::kill()
{
running_m.lock();
#ifndef NDEBUG
@@ -83,7 +80,7 @@ void Thread::kill(void)
}
#endif
#ifdef WINTHREAD
BOOL rc = TerminateThread(handle, 0);
BOOL rc = TerminateThread(handle, NULL);
assert(rc);
#else
int rc = pthread_cancel(id);
@@ -96,7 +93,7 @@ void Thread::kill(void)
/*
* Starts the thread
*/
void Thread::start(void)
void Thread::start()
{
#ifndef NDEBUG
// check whether the thread is already running
@@ -106,8 +103,8 @@ void Thread::start(void)
#endif
continue_m.lock();
#ifdef WINTHREAD
handle = CreateThread(0, 0, &the_thread, this, 0, &id);
assert(handle != 0);
handle = CreateThread(NULL, 0, &the_thread, this, 0, &id);
assert(handle != NULL);
#else
int rc = pthread_create(&id, 0, &the_thread, this);
assert(rc == 0);
@@ -145,10 +142,8 @@ void* Thread::the_thread(void *param)
#ifdef UNIT_TEST
// g++ -Wall -c mutex.cpp -o mutex.o
// g++ -Wall -DUNIT_TEST -c thread.cpp -o thread.o
// g++ -Wall -DUNIT_TEST mutex.o thread.o -o thread -lpthread
#include <iostream>
int main(void)
// g++ -Wall -DUNIT_TEST mutex.o thread.o -o thread -pthread
int main()
{
class Thread_test : public Thread
{
@@ -156,15 +151,14 @@ int main(void)
Thread_test(int testval)
: testval(testval) { }
int get_testval(void)
int get_testval()
{
testval_m.lock();
int rc = testval;
testval_m.unlock();
return rc;
}
void *thread(void)
void *thread()
{
// just do something
while (true) {
@@ -174,6 +168,7 @@ int main(void)
}
return 0;
}
private:
int testval;
Mutex testval_m;

View File

@@ -26,9 +26,13 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
// Modelled after JThread by Jori Liesenborgs
/*
* Modelled after JThread by Jori Liesenborgs
*/
#ifndef LIBSOCKTHREAD_THREAD_HPP
#define LIBSOCKTHREAD_THREAD_HPP
@@ -36,16 +40,16 @@
namespace Libsockthread {
class Thread {
public:
Thread(void)
: retval(0), running(false) { }
virtual ~Thread(void)
Thread()
: retval(NULL), running(false) { }
virtual ~Thread()
{ kill(); }
void* get_retval(void);
bool is_running(void);
void kill(void);
void start(void);
virtual void *thread(void) = 0;
void* get_retval();
bool is_running();
void kill();
void start();
virtual void* thread() = 0;
private:
#ifdef WINTHREAD
static DWORD WINAPI the_thread(void* param);
@@ -56,7 +60,7 @@ namespace Libsockthread {
pthread_t id;
#endif
Mutex continue_m;
void *retval;
void* retval;
bool running;
Mutex running_m;
};

View File

@@ -26,6 +26,8 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#include "platform.hpp"

View File

@@ -26,6 +26,8 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#ifndef LIBSOCKTHREAD_TIME_HPP
@@ -44,7 +46,6 @@ namespace Libsockthread {
string& utc();
string& utc_date();
string& utc_time();
private:
string formatted;
time_t unixtime;