1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
|
// Copyright (C) 2005-2009 Angelo Naselli, Penta Engineering s.r.l.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// Common C++. If you copy code from other releases into a copy of GNU
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//
#include <cc++/config.h>
#include <cc++/thread.h>
#include <cc++/slog.h>
#include <cc++/buffer.h>
#include <string>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
// TODO sc: test if has to move up now that it is into commoncpp
// NOTE: the order of inclusion is important do not move following include line
// redefinition of __EXPORT needed if we're compiling our dll
#include <cc++/export.h>
// local includes
#include <cc++/applog.h>
#ifdef CCXX_NAMESPACES
using namespace std;
namespace ost
{
#endif
class logStruct
{
public:
string _ident;
int _priority;
Slog::Level _level;
bool _enable;
bool _clogEnable;
bool _slogEnable;
size_t _msgpos;
enum logEnum
{
BUFF_SIZE = 512,
LAST_CHAR = BUFF_SIZE - 1
};
char _msgbuf[BUFF_SIZE];
logStruct() : _ident("") , _priority(Slog::levelDebug),
_level(Slog::levelDebug), _enable(false),
_clogEnable(false), _slogEnable(false), _msgpos(0)
{
memset(_msgbuf, 0, BUFF_SIZE);
};
~logStruct() {};
};
struct levelNamePair
{
const char *name;
Slog::Level level;
};
#ifdef WIN32
template class std::map<string, Slog::Level >;
#endif
class LevelName : public map<string, Slog::Level>
{
public:
LevelName(const levelNamePair initval[], int num)
{
for (int i = 0; i < num; i++)
insert(make_pair(initval[i].name, initval[i].level));
};
};
class logger : public ost::ThreadQueue
{
private:
string _nomeFile;
std::fstream _logfs;
bool _usePipe;
protected:
// to dequeue log messages and write them to file if not log_directly
virtual void runQueue(void *data);
virtual void startQueue(void);
virtual void stopQueue(void);
virtual void onTimer(void);
virtual void final(void);
public:
logger(const char* logFileName = NULL, bool usePipe = false);
virtual ~logger();
// To change log file name
void logFileName(const char* FileName, bool usePipe = false);
};
// mapping thread ID <-> logStruct (buffer)
typedef map <cctid_t, logStruct> LogPrivateData;
// map ident <-> levels
typedef map <string, Slog::Level> IdentLevel;
class AppLogPrivate
{
public:
// subscription and unsubsciption must be protected as well
ost::Mutex _subMutex;
// mapping thread ID <-> logStruct (buffer)
LogPrivateData _logs;
// map ident <-> levels
IdentLevel _identLevel;
// log directly into file
bool _logDirectly;
bool _logPipe;
// log spooler
logger *_pLogger;
string _nomeFile;
Mutex _lock;
std::fstream _logfs;
static const levelNamePair _values[];
static LevelName _assoc;
AppLogPrivate() : _pLogger(NULL) {}
~AppLogPrivate()
{
if (_pLogger)
delete _pLogger;
}
};
const levelNamePair AppLogPrivate::_values[] =
{
{ "emerg", Slog::levelEmergency },
{ "alert", Slog::levelAlert },
{ "critical", Slog::levelCritical },
{ "error", Slog::levelError },
{ "warn", Slog::levelWarning },
{ "notice", Slog::levelNotice },
{ "info", Slog::levelInfo },
{ "debug", Slog::levelDebug }
};
AppLog alog;
LevelName AppLogPrivate::_assoc(_values, sizeof AppLogPrivate::_values / sizeof *AppLogPrivate::_values);
map<string, Slog::Level> *AppLog::assoc = &AppLogPrivate::_assoc;
HEXdump::HEXdump(const unsigned char *buffer, int len, int max_len) : _str()
{
std::stringstream sstr;
if (buffer == NULL || len <= 0)
return ;
long buf_len = (max_len > 0 && len > max_len) ? max_len : len;
long int addr = 0;
int cnt2 = 0;
int n;
int i;
sstr.str("");
// get exception from ifstream failures
sstr.exceptions(ifstream::failbit | ifstream::badbit);
try
{
sstr << std::endl;
sstr << "dump " << len << " byte." << std::endl;
for (n = 0; n < buf_len; n++)
{
if (cnt2 == 0)
{
// Print address.
sstr << std::setw(7) << std::setfill('0') << int (addr) << " - ";
addr = addr + 16;
}
cnt2 = (cnt2 + 1) % 18;
if (cnt2 <= 16)
{
// print hex value
sstr << std::hex << std::setw(2) << std::setfill('0') << int (buffer[n]) << " ";
}
else
{
sstr << " ";
sstr << std::setfill(' ');
for (i = n - cnt2 + 1; i < n; i++)
{
// print ascii value
if (buffer[i] < 32 || 126 < buffer[i])
{
sstr << '.';
}
else
{
sstr << buffer[i];
}
}
sstr << std::endl;
sstr << std::dec;
cnt2 = 0;
n--;
}
}
sstr << std::setfill(' ');
for (i = cnt2 + 1; i <= 16 ; i++)
{
sstr << std::setw(2) << "--" << " ";
}
sstr << " ";
for (i = n - cnt2; cnt2 <= 16 && i < n; i++)
{
if (buffer[i] < 32 || 126 < buffer[i])
{
sstr << '.';
}
else
{
sstr << buffer[i];
}
}
sstr << std::dec;
if (max_len > 0 && len > max_len)
sstr << std::endl << "dump troncato a " << max_len << " byte." << std::endl;
}
catch (...)
{
sstr.str("HEXdump failed!");
}
_str = sstr.str();
}
// class logger
logger::logger(const char* logFileName, bool usePipe) : ThreadQueue(NULL, 0, 0), _usePipe(usePipe)
{
_nomeFile = "";
if (logFileName)
_nomeFile = logFileName;
if (!_nomeFile.empty())
{
if (!_usePipe)
{
_logfs.open(_nomeFile.c_str(), std::ofstream::out | std::ofstream::app | std::ofstream::ate);
}
#ifndef WIN32
else
{
// create pipe
int err = mkfifo(_nomeFile.c_str(), S_IRUSR | S_IWUSR);
if (err == 0 || errno == EEXIST)
{
// and open it
_logfs.open(_nomeFile.c_str(), std::fstream::in | std::fstream::out);
}
else
THROW(AppLogException("Can't create pipe"));
}
#endif
if (_logfs.fail())
THROW(AppLogException("Can't open log file name"));
}
}
logger::~logger()
{
Semaphore::post();
Thread::terminate();
_logfs.flush();
_logfs.close();
}
// New log file name
void logger::logFileName(const char* FileName, bool usePipe)
{
if (!FileName)
return;
_usePipe = usePipe;
_nomeFile = FileName;
if (_logfs.is_open())
_logfs.close();
if (!_nomeFile.empty())
{
if (!_usePipe)
{
_logfs.open(_nomeFile.c_str(), std::ofstream::out | std::ofstream::app | std::ofstream::ate);
}
#ifndef WIN32
else
{
// create pipe
int err = mkfifo(_nomeFile.c_str(), S_IRUSR | S_IWUSR);
if (err == 0 || errno == EEXIST)
{
// and open it
_logfs.open(_nomeFile.c_str(), std::fstream::in | std::fstream::out);
}
else
THROW(AppLogException("Can't create pipe"));
}
#endif
if (_logfs.fail())
THROW(AppLogException("Can't open log file name"));
}
}
// writes into filename enqueued messages
void logger::runQueue(void * data)
{
char *str = (char *) data;
if (_logfs.is_open())
{
Thread::setCancel(cancelDisabled);
_logfs << str;
_logfs.flush();
Thread::setCancel(cancelImmediate);
}
}
void logger::startQueue()
{
testCancel();
}
void logger::stopQueue()
{
testCancel();
}
void logger::onTimer()
{
}
void logger::final()
{
if (started)
{
data_t *pFirst = first;
while (pFirst)
{
runQueue(pFirst->data);
pFirst = pFirst->next;
}
}
}
#ifndef WIN32
AppLog::AppLog(const char* logFileName, bool logDirectly, bool usePipe) :
streambuf(), ostream((streambuf*) this)
#else
AppLog::AppLog(const char* logFileName, bool logDirectly) :
streambuf(), ostream((streambuf*) this)
#endif
{
d= NULL; // pedantic fussy about initing members before base classes...
d = new AppLogPrivate();
if (!d)
THROW(AppLogException("Memory allocation problem"));
d->_nomeFile = "";
d->_logDirectly = logDirectly;
#ifndef WIN32
d->_logPipe = usePipe;
#else
d->_logPipe = false;
#endif
// level string to level value
// assoc["emerg"] = levelEmergency;
// assoc["alert"] = levelAlert;
// assoc["critical"] = levelCritical;
// assoc["error"] = levelError;
// assoc["warn"] = levelWarning;
// assoc["notice"] = levelNotice;
// assoc["info"] = levelInfo;
// assoc["debug"] = levelDebug;
if (logFileName)
d->_nomeFile = logFileName;
if (!d->_logDirectly && logFileName)
d->_pLogger = new logger(logFileName, d->_logPipe);
else
d->_pLogger = NULL;
// writes to file directly
if (!d->_nomeFile.empty() && d->_logDirectly)
{
if (!d->_logPipe)
{
d->_logfs.open(d->_nomeFile.c_str(), std::fstream::in | std::fstream::out);
if (!d->_logfs.is_open())
{
d->_logfs.open(d->_nomeFile.c_str(), std::fstream::out | std::fstream::app);
}
else
d->_logfs.seekg(0, std::fstream::end);
}
// on Windows pipe are not used as they are not supported on WinNT
#ifndef WIN32
else
{
// create pipe
int err = mkfifo(d->_nomeFile.c_str(), S_IRUSR | S_IWUSR);
if (err == 0 || errno == EEXIST)
{
// and open it
d->_logfs.open(d->_nomeFile.c_str(), std::fstream::in | std::fstream::out);
}
else
THROW(AppLogException("Can't create pipe"));
}
#endif
if (d->_logfs.fail())
THROW(AppLogException("Can't open log file name"));
}
//from Error level on write to syslog also
slog.level(Slog::levelError);
slog.clogEnable(false);
}
AppLog::~AppLog()
{
// if _logDirectly
close();
if (d) delete d;
}
void AppLog::subscribe()
{
ost::MutexLock mtx(d->_subMutex);
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
{
// subscribes new thread
d->_logs[tid];
}
}
}
void AppLog::unsubscribe()
{
ost::MutexLock mtx(d->_subMutex);
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt != d->_logs.end())
{
// unsubscribes thread
d->_logs.erase(logIt);
}
}
}
#ifndef WIN32
void AppLog::logFileName(const char* FileName, bool logDirectly, bool usePipe)
#else
void AppLog::logFileName(const char* FileName, bool logDirectly)
#endif
{
if (!FileName)
{
slog.error("Null file name!");
return;
}
d->_lock.enterMutex();
d->_nomeFile = FileName;
close();
d->_logDirectly = logDirectly;
#ifndef WIN32
d->_logPipe = usePipe;
#else
d->_logPipe = false;
#endif
if (!d->_logDirectly)
{
d->_nomeFile = FileName;
if (d->_pLogger)
d->_pLogger->logFileName(FileName, d->_logPipe);
else
d->_pLogger = new logger(FileName, d->_logPipe);
d->_lock.leaveMutex();
return;
}
// log directly
if (!d->_nomeFile.empty())
{
if (!d->_logPipe)
{
d->_logfs.open(d->_nomeFile.c_str(), std::fstream::out | std::fstream::app);
}
#ifndef WIN32
else
{
// create pipe
int err = mkfifo(d->_nomeFile.c_str(), S_IRUSR | S_IWUSR);
if (err == 0 || errno == EEXIST)
{
// and open it
d->_logfs.open(d->_nomeFile.c_str(), std::fstream::in | std::fstream::out);
}
else
THROW(AppLogException("Can't create pipe"));
}
#endif
if (d->_logfs.fail())
THROW(AppLogException("Can't open log file name"));
}
d->_lock.leaveMutex();
}
// writes to log
void AppLog::writeLog(bool endOfLine)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
if ((d->_logDirectly && !d->_logfs.is_open() && !logIt->second._clogEnable) ||
(!d->_logDirectly && !d->_pLogger && !logIt->second._clogEnable))
{
logIt->second._msgpos = 0;
logIt->second._msgbuf[0] = '\0';
return;
}
if (logIt->second._enable)
{
time_t now;
struct tm *dt;
time(&now);
struct timeval detail_time;
gettimeofday(&detail_time, NULL);
dt = localtime(&now);
char buf[50];
const char *p = "unknown";
switch (logIt->second._priority)
{
case Slog::levelEmergency:
p = "emerg";
break;
case Slog::levelInfo:
p = "info";
break;
case Slog::levelError:
p = "error";
break;
case Slog::levelAlert:
p = "alert";
break;
case Slog::levelDebug:
p = "debug";
break;
case Slog::levelNotice:
p = "notice";
break;
case Slog::levelWarning:
p = "warn";
break;
case Slog::levelCritical:
p = "crit";
break;
}
snprintf(buf, sizeof(buf) - 1, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
dt->tm_year + 1900, dt->tm_mon + 1, dt->tm_mday,
dt->tm_hour, dt->tm_min, dt->tm_sec, (int)(detail_time.tv_usec / 1000));
buf[sizeof(buf)-1] = 0; // per sicurezza
if (d->_logDirectly)
{
d->_lock.enterMutex();
if (d->_logfs.is_open())
{
d->_logfs << buf;
if (!logIt->second._ident.empty())
d->_logfs << logIt->second._ident.c_str() << ": ";
d->_logfs << "[" << p << "] ";
d->_logfs << logIt->second._msgbuf;
if (endOfLine)
d->_logfs << endl;
d->_logfs.flush();
}
}
else if (d->_pLogger)
{
// ThreadQueue
std::stringstream sstr;
sstr.str(""); // reset contents
sstr << buf;
if (!logIt->second._ident.empty())
sstr << logIt->second._ident.c_str() << ": ";
sstr << "[" << p << "] ";
sstr << logIt->second._msgbuf;
if (endOfLine)
sstr << endl;
sstr.flush();
if (sstr.fail())
cerr << "stringstream failed!!!! " << endl;
// enqueues log message
d->_pLogger->post((void *) sstr.str().c_str(), sstr.str().length() + 1);
d->_lock.enterMutex();
}
// slog it if error level is right
if (logIt->second._slogEnable && logIt->second._priority <= Slog::levelError)
{
slog((Slog::Level) logIt->second._priority) << logIt->second._msgbuf;
if (endOfLine) slog << endl;
}
if (logIt->second._clogEnable
#ifndef WIN32
&& (getppid() > 1)
#endif
)
{
clog << logIt->second._msgbuf;
if (endOfLine)
clog << endl;
}
d->_lock.leaveMutex();
}
logIt->second._msgpos = 0;
logIt->second._msgbuf[0] = '\0';
}
}
void AppLog::close(void)
{
if (d->_logDirectly)
{
d->_lock.enterMutex();
if (d->_logfs.is_open())
{
d->_logfs.flush();
d->_logfs.close();
}
d->_lock.leaveMutex();
}
}
void AppLog::open(const char *ident)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
if (d->_nomeFile.empty())
{
std::cerr << "Empty file name" << std::endl;
slog.emerg("Empty file nane!\n");
}
if (d->_logDirectly)
{
d->_lock.enterMutex();
if (!d->_logfs.is_open())
{
d->_logfs.open(d->_nomeFile.c_str(), std::fstream::out | std::fstream::app);
}
if (!d->_logfs.is_open())
{
std::cerr << "Can't open file name!" << std::endl;
slog.emerg("Can't open file name!\n");
}
d->_lock.leaveMutex();
}
if (ident != NULL)
logIt->second._ident = ident;
}
}
void AppLog::identLevel(const char *ident, Slog::Level level)
{
if (!ident)
return;
string id = ident;
IdentLevel::iterator idLevIt = d->_identLevel.find(id);
if (idLevIt == d->_identLevel.end())
{
d->_identLevel[id] = level;
}
else
idLevIt->second = level;
}
void AppLog::level(Slog::Level enable)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
logIt->second._level = enable;
}
}
void AppLog::clogEnable(bool f)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
logIt->second._clogEnable = f;
}
}
void AppLog::slogEnable(bool en)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
logIt->second._slogEnable = en;
}
}
int AppLog::sync()
{
int retVal = (pbase() != pptr());
if (fail())
{
slog(Slog::levelNotice) << "fail() is true, calling clear()" << endl;
clear();
}
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt != d->_logs.end())
{
retVal = (logIt->second._msgpos > 0);
if (retVal)
{
slog(Slog::levelNotice) << "sync called and msgpos > 0" << endl;
}
}
}
overflow(EOF);
return retVal;
}
int AppLog::overflow(int c)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return c;
if (!logIt->second._enable)
return c;
if (c == '\n' || !c || c == EOF)
{
if (!logIt->second._msgpos)
{
if (c == '\n') writeLog(true);
return c;
}
if (logIt->second._msgpos < (int)(sizeof(logIt->second._msgbuf) - 1))
logIt->second._msgbuf[logIt->second._msgpos] = 0;
else
logIt->second._msgbuf[logIt->second._msgpos-1] = 0;
writeLog(c == '\n');
//reset buffer
logIt->second._msgpos = 0;
return c;
}
if (logIt->second._msgpos < (int)(sizeof(logIt->second._msgbuf) - 1))
logIt->second._msgbuf[logIt->second._msgpos++] = c;
}
return c;
}
#ifdef HAVE_SNPRINTF
void AppLog::error(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
error();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
if (logIt->second._slogEnable)
slog.error(logIt->second._msgbuf);
va_end(args);
}
}
void AppLog::warn(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
warn();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
if (logIt->second._slogEnable)
slog.warn(logIt->second._msgbuf);
va_end(args);
}
}
void AppLog::debug(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
debug();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
va_end(args);
}
}
void AppLog::emerg(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
emerg();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
if (logIt->second._slogEnable)
slog.emerg(logIt->second._msgbuf);
va_end(args);
}
}
void AppLog::alert(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
alert();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
if (logIt->second._slogEnable)
slog.alert(logIt->second._msgbuf);
va_end(args);
}
}
void AppLog::critical(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
critical();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
if (logIt->second._slogEnable)
slog.critical(logIt->second._msgbuf);
va_end(args);
}
}
void AppLog::notice(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
notice();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
if (logIt->second._slogEnable)
slog.notice(logIt->second._msgbuf);
va_end(args);
}
}
void AppLog::info(const char *format, ...)
{
va_list args;
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return;
info();
if (!logIt->second._enable)
return;
overflow(EOF);
va_start(args, format);
logIt->second._msgbuf[logStruct::BUFF_SIZE-1] = '\0';
logIt->second._msgpos = vsnprintf(logIt->second._msgbuf, logStruct::BUFF_SIZE, format, args);
if (logIt->second._msgpos > logStruct::BUFF_SIZE - 1) logIt->second._msgpos = logStruct::BUFF_SIZE - 1;
overflow(EOF);
va_end(args);
}
}
#endif //HAVE_SNPRINTF
AppLog &AppLog::operator()(Slog::Level lev)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return *this;
// needed? overflow(EOF);
// enables log
Slog::Level th_lev = logIt->second._level;
logIt->second._enable = (th_lev >= lev);
// is there a log level per module?
if (!logIt->second._ident.empty())
{
std::string st = logIt->second._ident;
IdentLevel::iterator idLevIt = d->_identLevel.find(st);
if (idLevIt != d->_identLevel.end())
{
th_lev = idLevIt->second;
logIt->second._enable = (th_lev >= lev);
}
}
logIt->second._priority = lev;
}
return *this;
}
AppLog &AppLog::operator()(const char *ident, Slog::Level lev)
{
Thread *pThr = getThread();
if (pThr)
{
cctid_t tid = pThr->getId();
LogPrivateData::iterator logIt = d->_logs.find(tid);
if (logIt == d->_logs.end())
return this->operator()(lev);
logIt->second._enable = true;
open(ident);
}
return this->operator()(lev);
}
AppLog& AppLog::operator<< (AppLog& (*pfManipulator)(AppLog&))
{
return (*pfManipulator)(*this);
}
AppLog& AppLog::operator<< (ostream& (*pfManipulator)(ostream&))
{
(*pfManipulator)(*(dynamic_cast<ostream*>(this)));
return *this ;
}
#ifdef CCXX_NAMESPACES
}
#endif
|