PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/acelite/ace/Log_Msg.cpp

https://github.com/chucho/FaceCore
C++ | 1441 lines | 986 code | 196 blank | 259 comment | 213 complexity | f690917c395ff68c77d3499ede44b9ca MD5 | raw file
  1. // $Id: Log_Msg.cpp 92052 2010-09-27 14:20:22Z vzykov $
  2. // We need this to get the status of ACE_NTRACE...
  3. #include "ace/config-all.h"
  4. // Turn off tracing for the duration of this file.
  5. #if defined (ACE_NTRACE)
  6. # undef ACE_NTRACE
  7. #endif /* ACE_NTRACE */
  8. #define ACE_NTRACE 1
  9. #include "ace/ACE.h"
  10. #include "ace/Thread_Manager.h"
  11. #include "ace/Guard_T.h"
  12. #include "ace/OS_NS_stdio.h"
  13. #include "ace/OS_NS_errno.h"
  14. #include "ace/OS_NS_sys_time.h"
  15. #include "ace/OS_NS_wchar.h"
  16. #include "ace/OS_NS_signal.h"
  17. #include "ace/os_include/os_typeinfo.h"
  18. #if !defined (ACE_MT_SAFE) || (ACE_MT_SAFE != 0)
  19. # include "ace/Object_Manager_Base.h"
  20. #endif /* ! ACE_MT_SAFE */
  21. #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
  22. // FUZZ: disable check_for_streams_include
  23. # include "ace/streams.h"
  24. #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
  25. #if defined (ACE_HAS_TRACE)
  26. # include "ace/Trace.h"
  27. #endif /* ACE_HAS_TRACE */
  28. #include "ace/Log_Msg.h"
  29. #include "ace/Log_Msg_Callback.h"
  30. #include "ace/Log_Msg_IPC.h"
  31. #include "ace/Log_Msg_NT_Event_Log.h"
  32. #include "ace/Log_Msg_UNIX_Syslog.h"
  33. #include "ace/Log_Record.h"
  34. #include "ace/Recursive_Thread_Mutex.h"
  35. #include "ace/Stack_Trace.h"
  36. #include "ace/Atomic_Op.h"
  37. #if !defined (__ACE_INLINE__)
  38. #include "ace/Log_Msg.inl"
  39. #endif /* __ACE_INLINE__ */
  40. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  41. ACE_ALLOC_HOOK_DEFINE(ACE_Log_Msg)
  42. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  43. bool ACE_Log_Msg::key_created_ = 0;
  44. # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
  45. defined (ACE_HAS_TSS_EMULATION)
  46. #if defined (ACE_MVS)
  47. static ACE_thread_key_t the_log_msg_tss_key =
  48. #if !defined(_LP64)
  49. { '\0','\0','\0','\0' };
  50. #else
  51. { '\0','\0','\0','\0','\0','\0','\0','\0' };
  52. #endif
  53. #else
  54. static ACE_thread_key_t the_log_msg_tss_key = 0;
  55. #endif /* defined (ACE_MVS) */
  56. ACE_thread_key_t *log_msg_tss_key (void)
  57. {
  58. return &the_log_msg_tss_key;
  59. }
  60. # endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
  61. #else
  62. static ACE_Cleanup_Adapter<ACE_Log_Msg>* log_msg_cleanup = 0;
  63. class ACE_Msg_Log_Cleanup: public ACE_Cleanup_Adapter<ACE_Log_Msg>
  64. {
  65. public:
  66. virtual ~ACE_Msg_Log_Cleanup (void) {
  67. if (this == log_msg_cleanup)
  68. log_msg_cleanup = 0;
  69. }
  70. };
  71. #endif /* ACE_MT_SAFE */
  72. #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE) && !defined (ACE_HAS_PHARLAP)
  73. # define ACE_LOG_MSG_SYSLOG_BACKEND ACE_Log_Msg_NT_Event_Log
  74. #elif !defined (ACE_LACKS_UNIX_SYSLOG) && !defined (ACE_HAS_WINCE)
  75. # define ACE_LOG_MSG_SYSLOG_BACKEND ACE_Log_Msg_UNIX_Syslog
  76. #else
  77. # define ACE_LOG_MSG_SYSLOG_BACKEND ACE_Log_Msg_IPC
  78. #endif /* ! ACE_WIN32 */
  79. // When doing ACE_OS::s[n]printf() calls in log(), we need to update
  80. // the space remaining in the output buffer based on what's returned from
  81. // the output function. If we could rely on more modern compilers, this
  82. // would be in an unnamed namespace, but it's a macro instead.
  83. // count is a size_t, len is an int and assumed to be non-negative.
  84. #define ACE_UPDATE_COUNT(COUNT, LEN) \
  85. do { if (static_cast<size_t> (LEN) > COUNT) COUNT = 0; \
  86. else COUNT -= static_cast<size_t> (LEN); \
  87. } while (0)
  88. /// Instance count for Log_Msg - used to know when dynamically
  89. /// allocated storage (program name and host name) can be safely
  90. /// deleted.
  91. int ACE_Log_Msg::instance_count_ = 0;
  92. /**
  93. * @class ACE_Log_Msg_Manager
  94. *
  95. * @brief Synchronize output operations.
  96. *
  97. * Provides global point of contact for all ACE_Log_Msg instances
  98. * in a process.
  99. *
  100. * For internal use by ACE, only!
  101. */
  102. class ACE_Log_Msg_Manager
  103. {
  104. public:
  105. static ACE_Log_Msg_Backend *log_backend_;
  106. static ACE_Log_Msg_Backend *custom_backend_;
  107. static u_long log_backend_flags_;
  108. static int init_backend (const u_long *flags = 0);
  109. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  110. //FUZZ: disable check_for_lack_ACE_OS
  111. static void close (void) ACE_GCC_DESTRUCTOR_ATTRIBUTE;
  112. //FUZZ: enable check_for_lack_ACE_OS
  113. static ACE_Recursive_Thread_Mutex *get_lock (void);
  114. private:
  115. static ACE_Recursive_Thread_Mutex *lock_;
  116. #endif /* ! ACE_MT_SAFE */
  117. };
  118. ACE_Log_Msg_Backend *ACE_Log_Msg_Manager::log_backend_ = 0;
  119. ACE_Log_Msg_Backend *ACE_Log_Msg_Manager::custom_backend_ = 0;
  120. u_long ACE_Log_Msg_Manager::log_backend_flags_ = 0;
  121. int ACE_Log_Msg_Manager::init_backend (const u_long *flags)
  122. {
  123. // If flags have been supplied, and they are different from the flags
  124. // we had last time, then we may have to re-create the backend as a
  125. // different type.
  126. if (flags)
  127. {
  128. // Sanity check for custom backend.
  129. if (ACE_BIT_ENABLED (*flags, ACE_Log_Msg::CUSTOM) &&
  130. ACE_Log_Msg_Manager::custom_backend_ == 0)
  131. {
  132. return -1;
  133. }
  134. if ((ACE_BIT_ENABLED (*flags, ACE_Log_Msg::SYSLOG)
  135. && ACE_BIT_DISABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG))
  136. || (ACE_BIT_DISABLED (*flags, ACE_Log_Msg::SYSLOG)
  137. && ACE_BIT_ENABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG)))
  138. {
  139. delete ACE_Log_Msg_Manager::log_backend_;
  140. ACE_Log_Msg_Manager::log_backend_ = 0;
  141. }
  142. ACE_Log_Msg_Manager::log_backend_flags_ = *flags;
  143. }
  144. if (ACE_Log_Msg_Manager::log_backend_ == 0)
  145. {
  146. ACE_NO_HEAP_CHECK;
  147. #if (defined (WIN32) || !defined (ACE_LACKS_UNIX_SYSLOG)) && !defined (ACE_HAS_WINCE) && !defined (ACE_HAS_PHARLAP)
  148. // Allocate the ACE_Log_Msg_Backend instance.
  149. if (ACE_BIT_ENABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG))
  150. ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_,
  151. ACE_LOG_MSG_SYSLOG_BACKEND,
  152. -1);
  153. else
  154. #endif /* defined (WIN32) && !defined (ACE_HAS_WINCE) && !defined (ACE_HAS_PHARLAP) */
  155. ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_,
  156. ACE_Log_Msg_IPC,
  157. -1);
  158. }
  159. return 0;
  160. }
  161. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  162. ACE_Recursive_Thread_Mutex *ACE_Log_Msg_Manager::lock_ = 0;
  163. ACE_Recursive_Thread_Mutex *
  164. ACE_Log_Msg_Manager::get_lock (void)
  165. {
  166. // This function is called by the first thread to create an ACE_Log_Msg
  167. // instance. It makes the call while holding a mutex, so we don't have
  168. // to grab another one here.
  169. if (ACE_Log_Msg_Manager::lock_ == 0)
  170. {
  171. ACE_NO_HEAP_CHECK;
  172. ACE_NEW_RETURN (ACE_Log_Msg_Manager::lock_,
  173. ACE_Recursive_Thread_Mutex,
  174. 0);
  175. }
  176. if (init_backend () == -1)
  177. return 0;
  178. return ACE_Log_Msg_Manager::lock_;
  179. }
  180. void
  181. ACE_Log_Msg_Manager::close (void)
  182. {
  183. #if defined (ACE_HAS_STHREADS) && ! defined (ACE_HAS_TSS_EMULATION) && ! defined (ACE_HAS_EXCEPTIONS)
  184. // Delete the (main thread's) Log_Msg instance. I think that this
  185. // is only "necessary" if exception handling is not enabled.
  186. // Without exception handling, main thread TSS destructors don't
  187. // seem to be called. It's not really necessary anyways, because
  188. // this one leak is harmless on Solaris.
  189. delete ACE_Log_Msg::instance ();
  190. #endif /* ACE_HAS_STHREADS && ! TSS_EMULATION && ! ACE_HAS_EXCEPTIONS */
  191. // Ugly, ugly, but don't know a better way.
  192. delete ACE_Log_Msg_Manager::lock_;
  193. ACE_Log_Msg_Manager::lock_ = 0;
  194. delete ACE_Log_Msg_Manager::log_backend_;
  195. ACE_Log_Msg_Manager::log_backend_ = 0;
  196. // we are never responsible for custom backend
  197. ACE_Log_Msg_Manager::custom_backend_ = 0;
  198. }
  199. # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
  200. defined (ACE_HAS_TSS_EMULATION)
  201. /* static */
  202. # if defined (ACE_HAS_THR_C_DEST)
  203. # define LOCAL_EXTERN_PREFIX extern "C"
  204. # else
  205. # define LOCAL_EXTERN_PREFIX
  206. # endif /* ACE_HAS_THR_C_DEST */
  207. LOCAL_EXTERN_PREFIX
  208. void
  209. ACE_TSS_CLEANUP_NAME (void *ptr)
  210. {
  211. // Delegate to thr_desc if this not has terminated
  212. ACE_Log_Msg* log_msg = (ACE_Log_Msg*) ptr;
  213. if (log_msg->thr_desc()!=0)
  214. log_msg->thr_desc()->log_msg_cleanup(log_msg);
  215. else
  216. delete log_msg;
  217. }
  218. # endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
  219. #endif /* ! ACE_MT_SAFE */
  220. /* static */
  221. int
  222. ACE_Log_Msg::exists (void)
  223. {
  224. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  225. # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
  226. defined (ACE_HAS_TSS_EMULATION)
  227. void *tss_log_msg = 0; // The actual type is ACE_Log_Msg*, but we need this
  228. // void to keep G++ from complaining.
  229. // Get the tss_log_msg from thread-specific storage.
  230. return ACE_Log_Msg::key_created_
  231. && ACE_Thread::getspecific (*(log_msg_tss_key ()), &tss_log_msg) != -1
  232. && tss_log_msg != 0;
  233. # else
  234. # error "Platform must support thread-specific storage if threads are used."
  235. # endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
  236. #else /* ! ACE_MT_SAFE */
  237. return 1;
  238. #endif /* ! ACE_MT_SAFE */
  239. }
  240. ACE_Log_Msg *
  241. ACE_Log_Msg::instance (void)
  242. {
  243. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  244. # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
  245. defined (ACE_HAS_TSS_EMULATION)
  246. // TSS Singleton implementation.
  247. if (!ACE_Log_Msg::key_created_)
  248. {
  249. ACE_thread_mutex_t *lock =
  250. reinterpret_cast<ACE_thread_mutex_t *> (
  251. ACE_OS_Object_Manager::preallocated_object
  252. [ACE_OS_Object_Manager::ACE_LOG_MSG_INSTANCE_LOCK]);
  253. if (1 == ACE_OS_Object_Manager::starting_up())
  254. //This function is called before ACE_OS_Object_Manager is
  255. //initialized. So the lock might not be valid. Assume it's
  256. //single threaded and so don't need the lock.
  257. ;
  258. else
  259. ACE_OS::thread_mutex_lock (lock);
  260. if (!ACE_Log_Msg::key_created_)
  261. {
  262. // Allocate the Singleton lock.
  263. ACE_Log_Msg_Manager::get_lock ();
  264. {
  265. ACE_NO_HEAP_CHECK;
  266. if (ACE_Thread::keycreate (log_msg_tss_key (),
  267. &ACE_TSS_CLEANUP_NAME) != 0)
  268. {
  269. if (1 == ACE_OS_Object_Manager::starting_up())
  270. //This function is called before ACE_OS_Object_Manager is
  271. //initialized. So the lock might not be valid. Assume it's
  272. //single threaded and so don't need the lock.
  273. ;
  274. else
  275. ACE_OS::thread_mutex_unlock (lock);
  276. return 0; // Major problems, this should *never* happen!
  277. }
  278. }
  279. ACE_Log_Msg::key_created_ = true;
  280. }
  281. if (1 == ACE_OS_Object_Manager::starting_up())
  282. //This function is called before ACE_OS_Object_Manager is
  283. //initialized. So the lock might not be valid. Assume it's
  284. //single threaded and so don't need the lock.
  285. ;
  286. else
  287. ACE_OS::thread_mutex_unlock (lock);
  288. }
  289. ACE_Log_Msg *tss_log_msg = 0;
  290. void *temp = 0;
  291. // Get the tss_log_msg from thread-specific storage.
  292. if (ACE_Thread::getspecific (*(log_msg_tss_key ()), &temp) == -1)
  293. return 0; // This should not happen!
  294. tss_log_msg = static_cast <ACE_Log_Msg *> (temp);
  295. // Check to see if this is the first time in for this thread.
  296. if (tss_log_msg == 0)
  297. {
  298. // Allocate memory off the heap and store it in a pointer in
  299. // thread-specific storage (on the stack...). Stop heap
  300. // checking, the memory will always be freed by the thread
  301. // rundown because of the TSS callback set up when the key was
  302. // created. This prevents from getting these blocks reported as
  303. // memory leaks.
  304. {
  305. ACE_NO_HEAP_CHECK;
  306. ACE_NEW_RETURN (tss_log_msg,
  307. ACE_Log_Msg,
  308. 0);
  309. // Store the dynamically allocated pointer in thread-specific
  310. // storage. It gets deleted via the ACE_TSS_cleanup function
  311. // when the thread terminates.
  312. if (ACE_Thread::setspecific (*(log_msg_tss_key()),
  313. reinterpret_cast<void *> (tss_log_msg))
  314. != 0)
  315. return 0; // Major problems, this should *never* happen!
  316. }
  317. }
  318. return tss_log_msg;
  319. # else
  320. # error "Platform must support thread-specific storage if threads are used."
  321. # endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
  322. #else /* ! ACE_MT_SAFE */
  323. // We don't have threads, we cannot call
  324. // ACE_Log_Msg_Manager::get_lock () to initialize the logger
  325. // callback, so instead we do it here.
  326. if (ACE_Log_Msg_Manager::init_backend () == -1)
  327. return 0;
  328. // Singleton implementation.
  329. if (log_msg_cleanup == 0)
  330. {
  331. ACE_NEW_RETURN (log_msg_cleanup, ACE_Msg_Log_Cleanup, 0);
  332. // Register the instance for destruction at program termination.
  333. ACE_Object_Manager::at_exit (log_msg_cleanup,
  334. 0,
  335. typeid (*log_msg_cleanup).name ());
  336. }
  337. return &log_msg_cleanup->object ();
  338. #endif /* ! ACE_MT_SAFE */
  339. }
  340. // Not inlined to help prevent having to include OS.h just to
  341. // get ACE_DEBUG, et al, macros.
  342. int
  343. ACE_Log_Msg::last_error_adapter (void)
  344. {
  345. return ACE_OS::last_error ();
  346. }
  347. // Sets the flag in the default priority mask used to initialize
  348. // ACE_Log_Msg instances, as well as the current per-thread instance.
  349. void
  350. ACE_Log_Msg::enable_debug_messages (ACE_Log_Priority priority)
  351. {
  352. ACE_SET_BITS (ACE_Log_Msg::default_priority_mask_, priority);
  353. ACE_Log_Msg *i = ACE_Log_Msg::instance ();
  354. i->priority_mask (i->priority_mask () | priority);
  355. }
  356. // Clears the flag in the default priority mask used to initialize
  357. // ACE_Log_Msg instances, as well as the current per-thread instance.
  358. void
  359. ACE_Log_Msg::disable_debug_messages (ACE_Log_Priority priority)
  360. {
  361. ACE_CLR_BITS (ACE_Log_Msg::default_priority_mask_, priority);
  362. ACE_Log_Msg *i = ACE_Log_Msg::instance ();
  363. i->priority_mask (i->priority_mask () & ~priority);
  364. }
  365. const ACE_TCHAR *
  366. ACE_Log_Msg::program_name (void)
  367. {
  368. return ACE_Log_Msg::program_name_;
  369. }
  370. /// Name of the local host.
  371. const ACE_TCHAR *ACE_Log_Msg::local_host_ = 0;
  372. /// Records the program name.
  373. const ACE_TCHAR *ACE_Log_Msg::program_name_ = 0;
  374. /// Default is to use stderr.
  375. u_long ACE_Log_Msg::flags_ = ACE_Log_Msg::STDERR;
  376. /// Process id of the current process.
  377. pid_t ACE_Log_Msg::pid_ = -2;
  378. /// Current offset of msg_[].
  379. ptrdiff_t ACE_Log_Msg::msg_off_ = 0;
  380. /// Default per-thread priority mask
  381. /// By default, no priorities are enabled.
  382. u_long ACE_Log_Msg::default_priority_mask_ = 0;
  383. /// Default per-process priority mask
  384. /// By default, all priorities are enabled.
  385. u_long ACE_Log_Msg::process_priority_mask_ = LM_SHUTDOWN
  386. | LM_TRACE
  387. | LM_DEBUG
  388. | LM_INFO
  389. | LM_NOTICE
  390. | LM_WARNING
  391. | LM_STARTUP
  392. | LM_ERROR
  393. | LM_CRITICAL
  394. | LM_ALERT
  395. | LM_EMERGENCY;
  396. void
  397. ACE_Log_Msg::close (void)
  398. {
  399. // This call needs to go here to avoid memory leaks.
  400. ACE_MT (ACE_Log_Msg_Manager::close ());
  401. // Please note that this will be called by a statement that is
  402. // harded coded into the ACE_Object_Manager's shutdown sequence, in
  403. // its destructor.
  404. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) && \
  405. (defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
  406. defined (ACE_HAS_TSS_EMULATION))
  407. if (ACE_Log_Msg::key_created_)
  408. {
  409. ACE_thread_mutex_t *lock =
  410. reinterpret_cast<ACE_thread_mutex_t *>
  411. (ACE_OS_Object_Manager::preallocated_object
  412. [ACE_OS_Object_Manager::ACE_LOG_MSG_INSTANCE_LOCK]);
  413. ACE_OS::thread_mutex_lock (lock);
  414. if (ACE_Log_Msg::key_created_)
  415. {
  416. // Clean up this ACE_Log_Msg instance and reset the TSS to
  417. // prevent any future cleanup attempts via TSS mechanisms at
  418. // thread exit. Otherwise in the event of a dynamic library
  419. // unload of libACE, by a program not linked with libACE,
  420. // ACE_TSS_cleanup will be invoked after libACE has been unloaded.
  421. // See Bugzilla 2980 for lots of details.
  422. ACE_Log_Msg *tss_log_msg = 0;
  423. void *temp = 0;
  424. // Get the tss_log_msg from thread-specific storage.
  425. if (ACE_Thread::getspecific (*(log_msg_tss_key ()), &temp) != -1
  426. && temp)
  427. {
  428. tss_log_msg = static_cast <ACE_Log_Msg *> (temp);
  429. // we haven't been cleaned up
  430. ACE_TSS_CLEANUP_NAME(tss_log_msg);
  431. if (ACE_Thread::setspecific(*(log_msg_tss_key()),
  432. reinterpret_cast <void *>(0)) != 0)
  433. ACE_OS::printf ("ACE_Log_Msg::close failed to ACE_Thread::setspecific to 0\n");
  434. }
  435. // The key is not needed any longer; ACE_Log_Msg is closing
  436. // and will need to be reopened if this process wishes to use
  437. // logging again. So delete the key.
  438. ACE_Thread::keyfree (*(log_msg_tss_key()));
  439. ACE_Log_Msg::key_created_ = false;
  440. }
  441. ACE_OS::thread_mutex_unlock (lock);
  442. }
  443. #endif /* (ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION) && ACE_MT_SAFE */
  444. }
  445. void
  446. ACE_Log_Msg::sync_hook (const ACE_TCHAR *prg_name)
  447. {
  448. ACE_LOG_MSG->sync (prg_name);
  449. }
  450. ACE_OS_Thread_Descriptor *
  451. ACE_Log_Msg::thr_desc_hook (void)
  452. {
  453. return ACE_LOG_MSG->thr_desc ();
  454. }
  455. // Call after a fork to resynchronize the PID and PROGRAM_NAME
  456. // variables.
  457. void
  458. ACE_Log_Msg::sync (const ACE_TCHAR *prog_name)
  459. {
  460. ACE_TRACE ("ACE_Log_Msg::sync");
  461. if (prog_name)
  462. {
  463. // Must free if already allocated!!!
  464. ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
  465. // Stop heap checking, block will be freed by the destructor when
  466. // the last ACE_Log_Msg instance is deleted.
  467. // Heap checking state will be restored when the block is left.
  468. {
  469. ACE_NO_HEAP_CHECK;
  470. ACE_Log_Msg::program_name_ = ACE_OS::strdup (prog_name);
  471. }
  472. }
  473. ACE_Log_Msg::pid_ = ACE_OS::getpid ();
  474. ACE_Log_Msg::msg_off_ = 0;
  475. }
  476. u_long
  477. ACE_Log_Msg::flags (void)
  478. {
  479. ACE_TRACE ("ACE_Log_Msg::flags");
  480. u_long result;
  481. ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
  482. *ACE_Log_Msg_Manager::get_lock (), 0));
  483. result = ACE_Log_Msg::flags_;
  484. return result;
  485. }
  486. void
  487. ACE_Log_Msg::set_flags (u_long flgs)
  488. {
  489. ACE_TRACE ("ACE_Log_Msg::set_flags");
  490. ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
  491. *ACE_Log_Msg_Manager::get_lock ()));
  492. ACE_SET_BITS (ACE_Log_Msg::flags_, flgs);
  493. }
  494. void
  495. ACE_Log_Msg::clr_flags (u_long flgs)
  496. {
  497. ACE_TRACE ("ACE_Log_Msg::clr_flags");
  498. ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
  499. *ACE_Log_Msg_Manager::get_lock ()));
  500. ACE_CLR_BITS (ACE_Log_Msg::flags_, flgs);
  501. }
  502. int
  503. ACE_Log_Msg::acquire (void)
  504. {
  505. ACE_TRACE ("ACE_Log_Msg::acquire");
  506. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  507. return ACE_Log_Msg_Manager::get_lock ()->acquire ();
  508. #else /* ! ACE_MT_SAFE */
  509. return 0;
  510. #endif /* ! ACE_MT_SAFE */
  511. }
  512. u_long
  513. ACE_Log_Msg::priority_mask (u_long n_mask, MASK_TYPE mask_type)
  514. {
  515. u_long o_mask;
  516. if (mask_type == THREAD)
  517. {
  518. o_mask = this->priority_mask_;
  519. this->priority_mask_ = n_mask;
  520. }
  521. else
  522. {
  523. o_mask = ACE_Log_Msg::process_priority_mask_;
  524. ACE_Log_Msg::process_priority_mask_ = n_mask;
  525. }
  526. return o_mask;
  527. }
  528. int
  529. ACE_Log_Msg::release (void)
  530. {
  531. ACE_TRACE ("ACE_Log_Msg::release");
  532. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  533. return ACE_Log_Msg_Manager::get_lock ()->release ();
  534. #else /* ! ACE_MT_SAFE */
  535. return 0;
  536. #endif /* ! ACE_MT_SAFE */
  537. }
  538. ACE_Log_Msg::ACE_Log_Msg (void)
  539. : status_ (0),
  540. errnum_ (0),
  541. linenum_ (0),
  542. msg_ (0),
  543. restart_ (1), // Restart by default...
  544. ostream_ (0),
  545. ostream_refcount_ (0),
  546. msg_callback_ (0),
  547. trace_depth_ (0),
  548. trace_active_ (false),
  549. tracing_enabled_ (true), // On by default?
  550. thr_desc_ (0),
  551. priority_mask_ (default_priority_mask_),
  552. timestamp_ (0)
  553. {
  554. // ACE_TRACE ("ACE_Log_Msg::ACE_Log_Msg");
  555. ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
  556. *ACE_Log_Msg_Manager::get_lock ()));
  557. ++instance_count_;
  558. if (this->instance_count_ == 1)
  559. ACE_Base_Thread_Adapter::set_log_msg_hooks (ACE_Log_Msg::init_hook,
  560. ACE_Log_Msg::inherit_hook,
  561. ACE_Log_Msg::close,
  562. ACE_Log_Msg::sync_hook,
  563. ACE_Log_Msg::thr_desc_hook);
  564. this->conditional_values_.is_set_ = false;
  565. char *timestamp = ACE_OS::getenv ("ACE_LOG_TIMESTAMP");
  566. if (timestamp != 0)
  567. {
  568. // If variable is set or is set to date tag so we print date and time.
  569. if (ACE_OS::strcmp (timestamp, "TIME") == 0)
  570. {
  571. this->timestamp_ = 1;
  572. }
  573. else if (ACE_OS::strcmp (timestamp, "DATE") == 0)
  574. {
  575. this->timestamp_ = 2;
  576. }
  577. }
  578. ACE_NEW_NORETURN (this->msg_, ACE_TCHAR[ACE_MAXLOGMSGLEN+1]);
  579. }
  580. ACE_Log_Msg::~ACE_Log_Msg (void)
  581. {
  582. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  583. int instance_count = 0;
  584. // Only hold the guard while updating the instance_count_.
  585. // If ACE_Log_Msg_Manager::close () is called, the lock will
  586. // be deleted.
  587. {
  588. ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
  589. *ACE_Log_Msg_Manager::get_lock ()));
  590. instance_count = --instance_count_;
  591. }
  592. // Release the guard.
  593. #else /* ! ACE_MT_SAFE */
  594. int instance_count = --instance_count_;
  595. #endif /* ! ACE_MT_SAFE */
  596. // If this is the last instance then cleanup. Only the last
  597. // thread to destroy its ACE_Log_Msg instance should execute
  598. // this block.
  599. if (instance_count == 0)
  600. {
  601. // Destroy the message queue instance.
  602. if (ACE_Log_Msg_Manager::log_backend_ != 0)
  603. ACE_Log_Msg_Manager::log_backend_->close ();
  604. // Close down custom backend
  605. if (ACE_Log_Msg_Manager::custom_backend_ != 0)
  606. ACE_Log_Msg_Manager::custom_backend_->close ();
  607. # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  608. # if defined (ACE_HAS_TSS_EMULATION)
  609. ACE_Log_Msg_Manager::close ();
  610. # endif /* ACE_HAS_TSS_EMULATION */
  611. # endif /* ACE_MT_SAFE */
  612. if (ACE_Log_Msg::program_name_)
  613. {
  614. ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
  615. ACE_Log_Msg::program_name_ = 0;
  616. }
  617. if (ACE_Log_Msg::local_host_)
  618. {
  619. ACE_OS::free ((void *) ACE_Log_Msg::local_host_);
  620. ACE_Log_Msg::local_host_ = 0;
  621. }
  622. }
  623. this->cleanup_ostream ();
  624. delete[] this->msg_;
  625. }
  626. void
  627. ACE_Log_Msg::cleanup_ostream ()
  628. {
  629. if (this->ostream_refcount_)
  630. {
  631. if (--*this->ostream_refcount_ == 0)
  632. {
  633. delete this->ostream_refcount_;
  634. #if defined (ACE_LACKS_IOSTREAM_TOTALLY)
  635. ACE_OS::fclose (this->ostream_);
  636. #else
  637. delete this->ostream_;
  638. this->ostream_ = 0;
  639. #endif
  640. }
  641. this->ostream_refcount_ = 0;
  642. }
  643. }
  644. // Open the sender-side of the message queue.
  645. int
  646. ACE_Log_Msg::open (const ACE_TCHAR *prog_name,
  647. u_long flags,
  648. const ACE_TCHAR *logger_key)
  649. {
  650. ACE_TRACE ("ACE_Log_Msg::open");
  651. ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
  652. *ACE_Log_Msg_Manager::get_lock (), -1));
  653. if (prog_name)
  654. {
  655. ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
  656. // Stop heap checking, block will be freed by the destructor.
  657. {
  658. ACE_NO_HEAP_CHECK;
  659. ACE_ALLOCATOR_RETURN (ACE_Log_Msg::program_name_,
  660. ACE_OS::strdup (prog_name),
  661. -1);
  662. }
  663. }
  664. else if (ACE_Log_Msg::program_name_ == 0)
  665. {
  666. // Stop heap checking, block will be freed by the destructor.
  667. ACE_NO_HEAP_CHECK;
  668. ACE_ALLOCATOR_RETURN (ACE_Log_Msg::program_name_,
  669. ACE_OS::strdup (ACE_TEXT ("<unknown>")),
  670. -1);
  671. }
  672. int status = 0;
  673. // Be sure that there is a message_queue_, with multiple threads.
  674. ACE_MT (ACE_Log_Msg_Manager::init_backend (&flags));
  675. // Always close the current handle before doing anything else.
  676. if (ACE_Log_Msg_Manager::log_backend_ != 0)
  677. ACE_Log_Msg_Manager::log_backend_->reset ();
  678. if (ACE_Log_Msg_Manager::custom_backend_ != 0)
  679. ACE_Log_Msg_Manager::custom_backend_->reset ();
  680. // Note that if we fail to open the message queue the default action
  681. // is to use stderr (set via static initialization in the
  682. // Log_Msg.cpp file).
  683. if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER)
  684. || ACE_BIT_ENABLED (flags, ACE_Log_Msg::SYSLOG))
  685. {
  686. // The SYSLOG backends (both NT and UNIX) can get along fine
  687. // without the logger_key - they will default to prog_name if
  688. // logger key is 0.
  689. if (logger_key == 0 && ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER))
  690. status = -1;
  691. else
  692. status = ACE_Log_Msg_Manager::log_backend_->open (logger_key);
  693. if (status == -1)
  694. ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::STDERR);
  695. else
  696. {
  697. if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER))
  698. ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER);
  699. if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::SYSLOG))
  700. ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::SYSLOG);
  701. }
  702. }
  703. else if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER)
  704. || ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::SYSLOG))
  705. {
  706. // If we are closing down logger, redirect logging to stderr.
  707. ACE_CLR_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER);
  708. ACE_CLR_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::SYSLOG);
  709. ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::STDERR);
  710. }
  711. if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::CUSTOM))
  712. {
  713. status =
  714. ACE_Log_Msg_Manager::custom_backend_->open (logger_key);
  715. if (status != -1)
  716. ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::CUSTOM);
  717. }
  718. // Remember, ACE_Log_Msg::STDERR bit is on by default...
  719. if (status != -1
  720. && ACE_BIT_ENABLED (flags,
  721. ACE_Log_Msg::STDERR) == 0)
  722. ACE_CLR_BITS (ACE_Log_Msg::flags_,
  723. ACE_Log_Msg::STDERR);
  724. // VERBOSE takes precedence over VERBOSE_LITE...
  725. if (ACE_BIT_ENABLED (flags,
  726. ACE_Log_Msg::VERBOSE_LITE))
  727. ACE_SET_BITS (ACE_Log_Msg::flags_,
  728. ACE_Log_Msg::VERBOSE_LITE);
  729. else if (ACE_BIT_ENABLED (flags,
  730. ACE_Log_Msg::VERBOSE))
  731. ACE_SET_BITS (ACE_Log_Msg::flags_,
  732. ACE_Log_Msg::VERBOSE);
  733. if (ACE_BIT_ENABLED (flags,
  734. ACE_Log_Msg::OSTREAM))
  735. {
  736. ACE_SET_BITS (ACE_Log_Msg::flags_,
  737. ACE_Log_Msg::OSTREAM);
  738. // Only set this to cerr if it hasn't already been set.
  739. if (this->msg_ostream () == 0)
  740. this->msg_ostream (ACE_DEFAULT_LOG_STREAM);
  741. }
  742. if (ACE_BIT_ENABLED (flags,
  743. ACE_Log_Msg::MSG_CALLBACK))
  744. ACE_SET_BITS (ACE_Log_Msg::flags_,
  745. ACE_Log_Msg::MSG_CALLBACK);
  746. if (ACE_BIT_ENABLED (flags,
  747. ACE_Log_Msg::SILENT))
  748. ACE_SET_BITS (ACE_Log_Msg::flags_,
  749. ACE_Log_Msg::SILENT);
  750. return status;
  751. }
  752. /**
  753. * Valid Options (prefixed by '%', as in printf format strings) include:
  754. * 'A': print an ACE_timer_t value
  755. * 'a': exit the program at this point (var-argument is the exit status!)
  756. * 'b': print a ssize_t value
  757. * 'B': print a size_t value
  758. * 'c': print a character
  759. * 'C': print a character string
  760. * 'i', 'd': print a decimal number
  761. * 'I', indent according to nesting depth
  762. * 'e', 'E', 'f', 'F', 'g', 'G': print a double
  763. * 'l', print line number where an error occurred.
  764. * 'M': print the name of the priority of the message.
  765. * 'm': Return the message corresponding to errno value, e.g., as done by <strerror>
  766. * 'N': print file name where the error occurred.
  767. * 'n': print the name of the program (or "<unknown>" if not set)
  768. * 'o': print as an octal number
  769. * 'P': format the current process id
  770. * 'p': format the appropriate errno message from sys_errlist, e.g., as done by <perror>
  771. * 'Q': print out the uint64 number
  772. * 'q': print out the int64 number
  773. * '@': print a void* pointer (in hexadecimal)
  774. * 'r': call the function pointed to by the corresponding argument
  775. * 'R': print return status
  776. * 'S': print out the appropriate signal message corresponding
  777. * to var-argument, e.g., as done by strsignal()
  778. * 's': format a character string
  779. * 'T': print timestamp in hour:minute:sec:usec format.
  780. * 'D': print timestamp in month/day/year hour:minute:sec:usec format.
  781. * 't': print thread id (1 if single-threaded)
  782. * 'u': print as unsigned int
  783. * 'x': print as a hex number
  784. * 'X': print as a hex number
  785. * 'w': print a wide character
  786. * 'W': print out a wide character string.
  787. * 'z': print an ACE_OS::WChar character
  788. * 'Z': print an ACE_OS::WChar character string
  789. * ':': print a time_t value as an integral number
  790. * '%': format a single percent sign, '%'
  791. */
  792. ssize_t
  793. ACE_Log_Msg::log (ACE_Log_Priority log_priority,
  794. const ACE_TCHAR *format_str, ...)
  795. {
  796. ACE_TRACE ("ACE_Log_Msg::log");
  797. // Start of variable args section.
  798. va_list argp;
  799. va_start (argp, format_str);
  800. ssize_t const result = this->log (format_str,
  801. log_priority,
  802. argp);
  803. va_end (argp);
  804. return result;
  805. }
  806. #if defined (ACE_HAS_WCHAR)
  807. /**
  808. * Since this is the ANTI_TCHAR version, we need to convert
  809. * the format string over.
  810. */
  811. ssize_t
  812. ACE_Log_Msg::log (ACE_Log_Priority log_priority,
  813. const ACE_ANTI_TCHAR *format_str, ...)
  814. {
  815. ACE_TRACE ("ACE_Log_Msg::log");
  816. // Start of variable args section.
  817. va_list argp;
  818. va_start (argp, format_str);
  819. ssize_t const result = this->log (ACE_TEXT_ANTI_TO_TCHAR (format_str),
  820. log_priority,
  821. argp);
  822. va_end (argp);
  823. return result;
  824. }
  825. #endif /* ACE_HAS_WCHAR */
  826. ssize_t
  827. ACE_Log_Msg::log (const ACE_TCHAR *format_str,
  828. ACE_Log_Priority log_priority,
  829. va_list argp)
  830. {
  831. ACE_TRACE ("ACE_Log_Msg::log");
  832. // External decls.
  833. typedef void (*PTF)(...);
  834. // Check if there were any conditional values set.
  835. bool const conditional_values = this->conditional_values_.is_set_;
  836. // Reset conditional values.
  837. this->conditional_values_.is_set_ = false;
  838. // Only print the message if <priority_mask_> hasn't been reset to
  839. // exclude this logging priority.
  840. if (this->log_priority_enabled (log_priority) == 0)
  841. return 0;
  842. // If conditional values were set and the log priority is correct,
  843. // then the values are actually set.
  844. if (conditional_values)
  845. this->set (this->conditional_values_.file_,
  846. this->conditional_values_.line_,
  847. this->conditional_values_.op_status_,
  848. this->conditional_values_.errnum_,
  849. this->restart (),
  850. this->msg_ostream (),
  851. this->msg_callback ());
  852. // Logging is supposed to be a benign activity (i.e., not interfer
  853. // with normal application operations), so don't inadvertently smash
  854. // errno!
  855. ACE_Errno_Guard guard (errno);
  856. ACE_Log_Record log_record (log_priority,
  857. ACE_OS::gettimeofday (),
  858. this->getpid ());
  859. // bp is pointer to where to put next part of logged message.
  860. // bspace is the number of characters remaining in msg_.
  861. ACE_TCHAR *bp = const_cast<ACE_TCHAR *> (this->msg ());
  862. size_t bspace = ACE_MAXLOGMSGLEN; // Leave room for Nul term.
  863. if (this->msg_off_ <= ACE_Log_Record::MAXLOGMSGLEN)
  864. bspace -= static_cast<size_t> (this->msg_off_);
  865. // If this platform has snprintf() capability to prevent overrunning the
  866. // output buffer, use it. To avoid adding a maintenance-hassle compile-
  867. // time couple between here and OS.cpp, don't try to figure this out at
  868. // compile time. Instead, do a quick check now; if we get a -1 return,
  869. // the platform doesn't support the length-limiting capability.
  870. ACE_TCHAR test[2];
  871. bool can_check = ACE_OS::snprintf (test, 1, ACE_TEXT ("x")) != -1;
  872. bool abort_prog = false;
  873. int exit_value = 0;
  874. if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::VERBOSE))
  875. {
  876. // Prepend the program name onto this message
  877. if (ACE_Log_Msg::program_name_ != 0)
  878. {
  879. for (const ACE_TCHAR *s = ACE_Log_Msg::program_name_;
  880. bspace > 1 && (*bp = *s) != '\0';
  881. ++s, --bspace)
  882. bp++;
  883. *bp++ = '|';
  884. --bspace;
  885. }
  886. }
  887. if (timestamp_ > 0)
  888. {
  889. ACE_TCHAR day_and_time[35];
  890. const ACE_TCHAR *s = 0;
  891. if (timestamp_ == 1)
  892. {
  893. // Print just the time
  894. s = ACE::timestamp (day_and_time, sizeof day_and_time / sizeof (ACE_TCHAR), 1);
  895. }
  896. else
  897. {
  898. // Print time and date
  899. ACE::timestamp (day_and_time, sizeof day_and_time / sizeof (ACE_TCHAR));
  900. s = day_and_time;
  901. }
  902. for (; bspace > 1 && (*bp = *s) != '\0'; ++s, --bspace)
  903. ++bp;
  904. *bp++ = '|';
  905. --bspace;
  906. }
  907. while (*format_str != '\0' && bspace > 0)
  908. {
  909. // Copy input to output until we encounter a %, however a
  910. // % followed by another % is not a format specification.
  911. if (*format_str != '%')
  912. {
  913. *bp++ = *format_str++;
  914. --bspace;
  915. }
  916. else if (format_str[1] == '%') // An "escaped" '%' (just print one '%').
  917. {
  918. *bp++ = *format_str++; // Store first %
  919. ++format_str; // but skip second %
  920. --bspace;
  921. }
  922. else
  923. {
  924. // This is most likely a format specification that ends with
  925. // one of the valid options described previously. To enable full
  926. // use of all sprintf capabilities, save the format specifier
  927. // from the '%' up to the format letter in a new char array.
  928. // This allows the full sprintf capability for padding, field
  929. // widths, alignment, etc. Any width/precision requiring a
  930. // caller-supplied argument is extracted and placed as text
  931. // into the format array. Lastly, we convert the caller-supplied
  932. // format specifier from the ACE_Log_Msg-supported list to the
  933. // equivalent sprintf specifier, and run the new format spec
  934. // through sprintf, adding it to the bp string.
  935. const ACE_TCHAR *abort_str = ACE_TEXT ("Aborting...");
  936. const ACE_TCHAR *start_format = format_str;
  937. ACE_TCHAR format[128]; // Converted format string
  938. ACE_TCHAR *fp; // Current format pointer
  939. int wp = 0; // Width/precision extracted from args
  940. bool done = false;
  941. bool skip_nul_locate = false;
  942. int this_len = 0; // How many chars s[n]printf wrote
  943. fp = format;
  944. *fp++ = *format_str++; // Copy in the %
  945. // Initialization to satisfy VC6
  946. int tmp_indent = 0;
  947. // Work through the format string to copy in the format
  948. // from the caller. While it's going across, extract ints
  949. // for '*' width/precision values from the argument list.
  950. // When the real format specifier is located, change it to
  951. // one recognized by sprintf, if needed, and do the sprintf
  952. // call.
  953. while (!done)
  954. {
  955. done = true; // Unless a conversion spec changes it
  956. switch (*format_str)
  957. {
  958. // The initial set of cases are the conversion
  959. // specifiers. Copy them in to the format array.
  960. // Note we don't use 'l', a normal conversion spec,
  961. // as a conversion because it is a ACE_Log_Msg format
  962. // specifier.
  963. case '-':
  964. case '+':
  965. case '0':
  966. case ' ':
  967. case '#':
  968. case '1':
  969. case '2':
  970. case '3':
  971. case '4':
  972. case '5':
  973. case '6':
  974. case '7':
  975. case '8':
  976. case '9':
  977. case '.':
  978. case 'L':
  979. case 'h':
  980. *fp++ = *format_str;
  981. done = false;
  982. break;
  983. case '*':
  984. wp = va_arg (argp, int);
  985. ACE_OS::sprintf (fp, ACE_TEXT ("%d"), wp);
  986. fp += ACE_OS::strlen (fp);
  987. done = false;
  988. break;
  989. case 'A': // ACE_timer_t
  990. {
  991. ACE_OS::strcpy (fp, ACE_TEXT ("f"));
  992. double const value = va_arg (argp, double);
  993. if (can_check)
  994. this_len = ACE_OS::snprintf (bp, bspace, format, value);
  995. else
  996. this_len = ACE_OS::sprintf (bp, format, value);
  997. ACE_UPDATE_COUNT (bspace, this_len);
  998. }
  999. break;
  1000. case 'a': // Abort program after handling all of format string.
  1001. abort_prog = true;
  1002. exit_value = va_arg (argp, int);
  1003. ACE_OS::strsncpy (bp, abort_str, bspace);
  1004. if (bspace > ACE_OS::strlen (abort_str))
  1005. bspace -= ACE_OS::strlen (abort_str);
  1006. else
  1007. bspace = 0;
  1008. break;
  1009. case 'l': // Source file line number
  1010. ACE_OS::strcpy (fp, ACE_TEXT ("d"));
  1011. if (can_check)
  1012. this_len = ACE_OS::snprintf (bp,
  1013. bspace,
  1014. format,
  1015. this->linenum ());
  1016. else
  1017. this_len = ACE_OS::sprintf (bp, format, this->linenum ());
  1018. ACE_UPDATE_COUNT (bspace, this_len);
  1019. break;
  1020. case 'N': // Source file name
  1021. #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
  1022. ACE_OS::strcpy (fp, ACE_TEXT ("ls"));
  1023. #else
  1024. ACE_OS::strcpy (fp, ACE_TEXT ("s"));
  1025. #endif
  1026. if (can_check)
  1027. this_len = ACE_OS::snprintf (bp, bspace, format,
  1028. this->file () ?
  1029. ACE_TEXT_CHAR_TO_TCHAR (this->file ())
  1030. : ACE_TEXT ("<unknown file>"));
  1031. else
  1032. this_len = ACE_OS::sprintf (bp, format,
  1033. this->file () ?
  1034. ACE_TEXT_CHAR_TO_TCHAR (this->file ())
  1035. : ACE_TEXT ("<unknown file>"));
  1036. ACE_UPDATE_COUNT (bspace, this_len);
  1037. break;
  1038. case 'n': // Program name
  1039. #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
  1040. ACE_OS::strcpy (fp, ACE_TEXT ("ls"));
  1041. #else /* ACE_WIN32 && ACE_USES_WCHAR */
  1042. ACE_OS::strcpy (fp, ACE_TEXT ("s"));
  1043. #endif
  1044. if (can_check)
  1045. this_len = ACE_OS::snprintf (bp, bspace, format,
  1046. ACE_Log_Msg::program_name_ ?
  1047. ACE_Log_Msg::program_name_ :
  1048. ACE_TEXT ("<unknown>"));
  1049. else
  1050. this_len = ACE_OS::sprintf (bp, format,
  1051. ACE_Log_Msg::program_name_ ?
  1052. ACE_Log_Msg::program_name_ :
  1053. ACE_TEXT ("<unknown>"));
  1054. ACE_UPDATE_COUNT (bspace, this_len);
  1055. break;
  1056. case 'P': // Process ID
  1057. #if defined (ACE_OPENVMS)
  1058. // Print the process id in hex on OpenVMS.
  1059. ACE_OS::strcpy (fp, ACE_TEXT ("x"));
  1060. #else
  1061. ACE_OS::strcpy (fp, ACE_TEXT ("d"));
  1062. #endif
  1063. if (can_check)
  1064. this_len = ACE_OS::snprintf
  1065. (bp, bspace, format,
  1066. static_cast<int> (this->getpid ()));
  1067. else
  1068. this_len = ACE_OS::sprintf
  1069. (bp, format, static_cast<int> (this->getpid ()));
  1070. ACE_UPDATE_COUNT (bspace, this_len);
  1071. break;
  1072. case 'p': // <errno> string, ala perror()
  1073. {
  1074. errno = 0;
  1075. char *msg = ACE_OS::strerror (ACE::map_errno (this->errnum ()));
  1076. // Windows can try to translate the errnum using
  1077. // system calls if strerror() doesn't get anything useful.
  1078. #if defined (ACE_WIN32)
  1079. if (errno == 0)
  1080. {
  1081. #endif
  1082. #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
  1083. ACE_OS::strcpy (fp, ACE_TEXT ("ls: %ls"));
  1084. wchar_t *str = va_arg (argp, wchar_t *);
  1085. #else
  1086. ACE_OS::strcpy (fp, ACE_TEXT ("s: %s"));
  1087. ACE_TCHAR *str = va_arg (argp, ACE_TCHAR *);
  1088. #endif
  1089. if (can_check)
  1090. this_len = ACE_OS::snprintf
  1091. (bp, bspace, format,
  1092. str ? str : ACE_TEXT ("(null)"),
  1093. ACE_TEXT_CHAR_TO_TCHAR (msg));
  1094. else
  1095. this_len = ACE_OS::sprintf
  1096. (bp, format,
  1097. str ? str : ACE_TEXT ("(null)"),
  1098. ACE_TEXT_CHAR_TO_TCHAR (msg));
  1099. #if defined (ACE_WIN32)
  1100. }
  1101. else
  1102. {
  1103. errno = ACE::map_errno (this->errnum ());
  1104. ACE_TCHAR *lpMsgBuf = 0;
  1105. // PharLap can't do FormatMessage, so try for socket
  1106. // error.
  1107. # if !defined (ACE_HAS_PHARLAP)
  1108. ACE_TEXT_FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
  1109. | FORMAT_MESSAGE_MAX_WIDTH_MASK
  1110. | FORMAT_MESSAGE_FROM_SYSTEM,
  1111. 0,
  1112. errno,
  1113. MAKELANGID (LANG_NEUTRAL,
  1114. SUBLANG_DEFAULT),
  1115. // Default language
  1116. (ACE_TCHAR *) &lpMsgBuf,
  1117. 0,
  1118. 0);
  1119. # endif /* ACE_HAS_PHARLAP */
  1120. // If we don't get a valid response from
  1121. // <FormatMessage>, we'll assume this is a
  1122. // WinSock error and so we'll try to convert
  1123. // it into a string. If this doesn't work it
  1124. // returns "unknown error" which is fine for
  1125. // our purposes.
  1126. ACE_TCHAR *str = va_arg (argp, ACE_TCHAR *);
  1127. if (lpMsgBuf == 0)
  1128. {
  1129. const ACE_TCHAR *message =
  1130. ACE::sock_error (errno);
  1131. ACE_OS::strcpy (fp, ACE_TEXT ("s: %s"));
  1132. if (can_check)
  1133. this_len = ACE_OS::snprintf
  1134. (bp, bspace, format,
  1135. str ? str : ACE_TEXT ("(null)"),
  1136. message);
  1137. else
  1138. this_len = ACE_OS::sprintf
  1139. (bp, format,
  1140. str ? str : ACE_TEXT ("(null)"),
  1141. message);
  1142. }
  1143. else
  1144. {
  1145. ACE_OS::strcpy (fp, ACE_TEXT ("s: %s"));
  1146. if (can_check)
  1147. this_len = ACE_OS::snprintf
  1148. (bp, bspace, format,
  1149. str ? str : ACE_TEXT ("(null)"),
  1150. lpMsgBuf);
  1151. else
  1152. this_len = ACE_OS::sprintf
  1153. (bp, format,
  1154. str ? str : ACE_TEXT ("(null)"),
  1155. lpMsgBuf);
  1156. // Free the buffer.
  1157. ::LocalFree (lpMsgBuf);
  1158. }
  1159. }
  1160. #endif /* ACE_WIN32 */
  1161. ACE_UPDATE_COUNT (bspace, this_len);
  1162. break;
  1163. }
  1164. case 'M': // Print the name of the priority of the message.
  1165. // Look at the format precision specifier. .1 is interpreted
  1166. // as a single character printout, otherwise we print the name of
  1167. // the priority.
  1168. // So, did we find a .1 specifier? Do we need to override it?
  1169. if (format[1] == ACE_TEXT('.') &&
  1170. format[2] == ACE_TEXT('1'))
  1171. {
  1172. // Yup.
  1173. // Print a single character signifying the severity of the message
  1174. fp = format;
  1175. fp++;
  1176. # if defined (ACE_USES_WCHAR)
  1177. # if defined (ACE_WIN32) // Windows uses 'c' for a wide character
  1178. ACE_OS::strcpy (fp, ACE_TEXT ("c"));
  1179. # else // Other platforms behave differently
  1180. # if defined (HPUX) // HP-Unix compatible
  1181. ACE_OS::strcpy (fp, ACE_TEXT ("C"));
  1182. # else // Other
  1183. ACE_OS::strcpy (fp, ACE_TEXT ("lc"));
  1184. # endif /* HPUX */
  1185. # endif
  1186. # else /* ACE_USES_WCHAR */
  1187. // Non-unicode builds simply use a standard character format specifier
  1188. ACE_OS::strcpy (fp, ACE_TEXT ("c"));
  1189. # endif /* ACE_USES_WCHAR */
  1190. // Below is an optimized (binary search based)
  1191. // version of the following simple piece of code:
  1192. //
  1193. // log_priority == LM_SHUTDOWN ? 'S' : // Shutdown
  1194. // log_priority == LM_TRACE ? 'T' : // Trace
  1195. // log_priority == LM_DEBUG ? 'D' : // Debug
  1196. // log_priority == LM_INFO ? 'I' : // Info
  1197. // log_priority == LM_NOTICE ? 'N' : // Notice
  1198. // log_priority == LM_WARNING ? 'W' : // Warning
  1199. // log_priority == LM_STARTUP ? 'U' : // Startup
  1200. // log_priority == LM_ERROR ? 'E' : // Error
  1201. // log_priority == LM_CRITICAL ? 'C' : // Critical
  1202. // log_priority == LM_ALERT ? 'A' : // Alert
  1203. // log_priority == LM_EMERGENCY ? '!' : // Emergency
  1204. // '?' // Unknown
  1205. if (can_check)
  1206. {
  1207. this_len = ACE_OS::snprintf
  1208. (bp, bspace, format,
  1209. #if !defined (ACE_USES_WCHAR) || defined (ACE_WIN32)
  1210. (int)
  1211. #else
  1212. (wint_t)
  1213. #endif
  1214. (log_priority <= LM_WARNING) ?
  1215. (log_priority <= LM_DEBUG) ?
  1216. (log_priority <= LM_TRACE) ?
  1217. (log_priority == LM_SHUTDOWN) ?
  1218. ACE_TEXT('S') : ACE_TEXT('T') : ACE_TEXT('D') :
  1219. (log_priority <= LM_NOTICE) ?
  1220. (log_priority == LM_INFO) ?
  1221. ACE_TEXT('I') : ACE_TEXT('N') : ACE_TEXT('W') :
  1222. (log_priority <= LM_CRITICAL) ?
  1223. (log_priority <= LM_ERROR) ?
  1224. (log_priority == LM_STARTUP) ?
  1225. ACE_TEXT('U') : ACE_TEXT('E') : ACE_TEXT('C') :
  1226. (log_priority <= LM_EMERGENCY) ?
  1227. (log_priority == LM_ALERT) ?
  1228. ACE_TEXT('A') : ACE_TEXT('!') : ACE_TEXT('?'));
  1229. }
  1230. else
  1231. {
  1232. this_len = ACE_OS::sprintf
  1233. (bp, format,
  1234. #if !defined (ACE_USES_WCHAR) || defined (ACE_WIN32)
  1235. (int)
  1236. #else
  1237. (wint_t)
  1238. #endif
  1239. (log_priority <= LM_WARNING) ?
  1240. (log_priority <= LM_DEBUG) ?
  1241. (log_priority <= LM_TRACE) ?
  1242. (log_priority == LM_SHUTDOWN) ?
  1243. ACE_TEXT('S') : ACE_TEXT('T') : ACE_TEXT('D') :
  1244. (log_priority <= LM_NOTICE) ?
  1245. (log_pri