LogSink.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifdef __win32__
00018 #include <io.h>
00019 #else
00020 // XXX/bowei
00021 #endif
00022 
00023 #include "LogSink.h"
00024 
00025 namespace oasys {
00026 
00027 //----------------------------------------------------------------------------
00028 FileLogSink::FileLogSink(const char* filename)
00029     : fd_(0)
00030 {
00031     strcpy(filename_, filename);
00032 }
00033 
00034 //----------------------------------------------------------------------------
00035 void
00036 FileLogSink::rotate()
00037 { 
00038     // XXX/bowei -- TODO
00039 }
00040 
00041 //----------------------------------------------------------------------------
00042 void
00043 FileLogSink::log(const char* str)
00044 {
00045     // Porting note: We use the raw functions here because the IO
00046     // functions use logging. Have to start somewhere.
00047 #ifdef __win32__
00048     _write(fd_, str, strlen(str));
00049 #else
00050 #endif
00051 }
00052 
00053 
00054 //----------------------------------------------------------------------------
00055 RingBufferLogSink::RingBufferLogSink()
00056     : cur_buf_(0)
00057 {
00058     memset(static_buf_, 0, NUM_BUFFERS * BUF_LEN + 16);
00059 
00060     // Guards for easy finding upon a core dump
00061     memcpy(static_buf_, "|RNGBUF|", 8);
00062     memcpy(static_buf_ + NUM_BUFFERS * BUF_LEN + 8, "|RNGBUF|", 8);
00063 
00064     buf_ = static_buf_ + 8;
00065 }
00066 
00067 //----------------------------------------------------------------------------
00068 void
00069 RingBufferLogSink::log(const char* str)
00070 {
00071     cur_buf_  = (cur_buf_ + 1) % NUM_BUFFERS;
00072     char* buf = at(cur_buf_);
00073     
00074     size_t len = strlen(str);
00075     if (len > BUF_LEN - 4) {
00076         len = BUF_LEN - 4;
00077         memcpy(cur_buf_, "...", 4);
00078     } else {
00079         cur_buf_[len] = '\0';
00080     }
00081     memcpy(cur_buf_, str, len);
00082 }
00083 
00084 //----------------------------------------------------------------------------
00085 
00086 } // namespace oasys

Generated on Thu Jun 7 16:56:50 2007 for DTN Reference Implementation by  doxygen 1.5.1