1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef _LOG4CXX_MESSAGE_BUFFER_H 19 #define _LOG4CXX_MESSAGE_BUFFER_H 20 21 #include <log4cxx/log4cxx.h> 22 #include <log4cxx/logstring.h> 23 #include <sstream> 24 25 #if defined(_MSC_VER) 26 #pragma warning ( push ) 27 #pragma warning ( disable: 4251 4275 ) 28 #endif 29 30 namespace log4cxx 31 { 32 33 34 namespace helpers 35 { 36 37 void MessageBufferUseStaticStream(); 38 39 typedef std::ios_base& (*ios_base_manip)(std::ios_base&); 40 41 /** 42 * This class is used by the LOG4CXX_INFO and similar 43 * macros to support insertion operators in the message parameter. 44 * The class is not intended for use outside of that context. 45 */ 46 class LOG4CXX_EXPORT CharMessageBuffer 47 { 48 public: 49 /** 50 * Creates a new instance. 51 */ 52 CharMessageBuffer(); 53 /** 54 * Destructor. 55 */ 56 ~CharMessageBuffer(); 57 58 59 /** 60 * Appends string to buffer. 61 * @param msg string append. 62 * @return this buffer. 63 */ 64 CharMessageBuffer& operator<<(const std::basic_string<char>& msg); 65 /** 66 * Appends string to buffer. 67 * @param msg string to append. 68 * @return this buffer. 69 */ 70 CharMessageBuffer& operator<<(const char* msg); 71 /** 72 * Appends string to buffer. 73 * @param msg string to append. 74 * @return this buffer. 75 */ 76 CharMessageBuffer& operator<<(char* msg); 77 78 /** 79 * Appends character to buffer. 80 * @param msg character to append. 81 * @return this buffer. 82 */ 83 CharMessageBuffer& operator<<(const char msg); 84 85 /** 86 * Insertion operator for STL manipulators such as std::fixed. 87 * @param manip manipulator. 88 * @return encapsulated STL stream. 89 */ 90 std::ostream& operator<<(ios_base_manip manip); 91 /** 92 * Insertion operator for built-in type. 93 * @param val build in type. 94 * @return encapsulated STL stream. 95 */ 96 std::ostream& operator<<(bool val); 97 98 /** 99 * Insertion operator for built-in type. 100 * @param val build in type. 101 * @return encapsulated STL stream. 102 */ 103 std::ostream& operator<<(short val); 104 /** 105 * Insertion operator for built-in type. 106 * @param val build in type. 107 * @return encapsulated STL stream. 108 */ 109 std::ostream& operator<<(int val); 110 /** 111 * Insertion operator for built-in type. 112 * @param val build in type. 113 * @return encapsulated STL stream. 114 */ 115 std::ostream& operator<<(unsigned int val); 116 /** 117 * Insertion operator for built-in type. 118 * @param val build in type. 119 * @return encapsulated STL stream. 120 */ 121 std::ostream& operator<<(long val); 122 /** 123 * Insertion operator for built-in type. 124 * @param val build in type. 125 * @return encapsulated STL stream. 126 */ 127 std::ostream& operator<<(unsigned long val); 128 /** 129 * Insertion operator for built-in type. 130 * @param val build in type. 131 * @return encapsulated STL stream. 132 */ 133 std::ostream& operator<<(float val); 134 /** 135 * Insertion operator for built-in type. 136 * @param val build in type. 137 * @return encapsulated STL stream. 138 */ 139 std::ostream& operator<<(double val); 140 /** 141 * Insertion operator for built-in type. 142 * @param val build in type. 143 * @return encapsulated STL stream. 144 */ 145 std::ostream& operator<<(long double val); 146 /** 147 * Insertion operator for built-in type. 148 * @param val build in type. 149 * @return encapsulated STL stream. 150 */ 151 std::ostream& operator<<(void* val); 152 153 /** 154 * Cast to ostream. 155 */ 156 operator std::basic_ostream<char>& (); 157 158 /** 159 * Get content of buffer. 160 * @param os used only to signal that 161 * the embedded stream was used. 162 */ 163 const std::basic_string<char>& str(std::basic_ostream<char>& os); 164 165 /** 166 * Get content of buffer. 167 * @param buf used only to signal that 168 * the embedded stream was not used. 169 */ 170 const std::basic_string<char>& str(CharMessageBuffer& buf); 171 172 /** 173 * Returns true if buffer has an encapsulated STL stream. 174 * @return true if STL stream was created. 175 */ 176 bool hasStream() const; 177 178 private: 179 /** 180 * Prevent use of default copy constructor. 181 */ 182 CharMessageBuffer(const CharMessageBuffer&); 183 /** 184 * Prevent use of default assignment operator. 185 */ 186 CharMessageBuffer& operator=(const CharMessageBuffer&); 187 188 /** 189 * Encapsulated std::string. 190 */ 191 std::basic_string<char> buf; 192 /** 193 * Encapsulated stream, created on demand. 194 */ 195 std::basic_ostringstream<char>* stream; 196 }; 197 198 template<class V> 199 std::basic_ostream<char>& operator<<(CharMessageBuffer& os, const V& val) 200 { 201 return ((std::basic_ostream<char>&) os) << val; 202 } 203 204 #if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API || LOG4CXX_LOGCHAR_IS_UNICHAR 205 /** 206 * This class is designed to support insertion operations 207 * in the message argument to the LOG4CXX_INFO and similar 208 * macros and is not designed for general purpose use. 209 */ 210 class LOG4CXX_EXPORT UniCharMessageBuffer 211 { 212 public: 213 /** 214 * Creates a new instance. 215 */ 216 UniCharMessageBuffer(); 217 /** 218 * Destructor. 219 */ 220 ~UniCharMessageBuffer(); 221 222 typedef std::basic_ostream<UniChar> uostream; 223 224 225 /** 226 * Appends string to buffer. 227 * @param msg string append. 228 * @return this buffer. 229 */ 230 UniCharMessageBuffer& operator<<(const std::basic_string<UniChar>& msg); 231 /** 232 * Appends string to buffer. 233 * @param msg string to append. 234 * @return this buffer. 235 */ 236 UniCharMessageBuffer& operator<<(const UniChar* msg); 237 /** 238 * Appends string to buffer. 239 * @param msg string to append. 240 * @return this buffer. 241 */ 242 UniCharMessageBuffer& operator<<(UniChar* msg); 243 244 /** 245 * Appends character to buffer. 246 * @param msg character to append. 247 * @return this buffer. 248 */ 249 UniCharMessageBuffer& operator<<(const UniChar msg); 250 251 #if LOG4CXX_CFSTRING_API 252 /** 253 * Appends a string into the buffer and 254 * fixes the buffer to use char characters. 255 * @param msg message to append. 256 * @return encapsulated CharMessageBuffer. 257 */ 258 UniCharMessageBuffer& operator<<(const CFStringRef& msg); 259 #endif 260 261 /** 262 * Insertion operator for STL manipulators such as std::fixed. 263 * @param manip manipulator. 264 * @return encapsulated STL stream. 265 */ 266 uostream& operator<<(ios_base_manip manip); 267 /** 268 * Insertion operator for built-in type. 269 * @param val build in type. 270 * @return encapsulated STL stream. 271 */ 272 uostream& operator<<(bool val); 273 274 /** 275 * Insertion operator for built-in type. 276 * @param val build in type. 277 * @return encapsulated STL stream. 278 */ 279 uostream& operator<<(short val); 280 /** 281 * Insertion operator for built-in type. 282 * @param val build in type. 283 * @return encapsulated STL stream. 284 */ 285 uostream& operator<<(int val); 286 /** 287 * Insertion operator for built-in type. 288 * @param val build in type. 289 * @return encapsulated STL stream. 290 */ 291 uostream& operator<<(unsigned int val); 292 /** 293 * Insertion operator for built-in type. 294 * @param val build in type. 295 * @return encapsulated STL stream. 296 */ 297 uostream& operator<<(long val); 298 /** 299 * Insertion operator for built-in type. 300 * @param val build in type. 301 * @return encapsulated STL stream. 302 */ 303 uostream& operator<<(unsigned long val); 304 /** 305 * Insertion operator for built-in type. 306 * @param val build in type. 307 * @return encapsulated STL stream. 308 */ 309 uostream& operator<<(float val); 310 /** 311 * Insertion operator for built-in type. 312 * @param val build in type. 313 * @return encapsulated STL stream. 314 */ 315 uostream& operator<<(double val); 316 /** 317 * Insertion operator for built-in type. 318 * @param val build in type. 319 * @return encapsulated STL stream. 320 */ 321 uostream& operator<<(long double val); 322 /** 323 * Insertion operator for built-in type. 324 * @param val build in type. 325 * @return encapsulated STL stream. 326 */ 327 uostream& operator<<(void* val); 328 329 330 /** 331 * Cast to ostream. 332 */ 333 operator uostream& (); 334 335 /** 336 * Get content of buffer. 337 * @param os used only to signal that 338 * the embedded stream was used. 339 */ 340 const std::basic_string<UniChar>& str(uostream& os); 341 342 /** 343 * Get content of buffer. 344 * @param buf used only to signal that 345 * the embedded stream was not used. 346 */ 347 const std::basic_string<UniChar>& str(UniCharMessageBuffer& buf); 348 349 /** 350 * Returns true if buffer has an encapsulated STL stream. 351 * @return true if STL stream was created. 352 */ 353 bool hasStream() const; 354 355 private: 356 /** 357 * Prevent use of default copy constructor. 358 */ 359 UniCharMessageBuffer(const UniCharMessageBuffer&); 360 /** 361 * Prevent use of default assignment operator. 362 */ 363 UniCharMessageBuffer& operator=(const UniCharMessageBuffer&); 364 365 /** 366 * Encapsulated std::string. 367 */ 368 std::basic_string<UniChar> buf; 369 /** 370 * Encapsulated stream, created on demand. 371 */ 372 std::basic_ostringstream<UniChar>* stream; 373 }; 374 375 template<class V> 376 UniCharMessageBuffer::uostream& operator<<(UniCharMessageBuffer& os, const V& val) 377 { 378 return ((UniCharMessageBuffer::uostream&) os) << val; 379 } 380 #endif 381 382 #if LOG4CXX_WCHAR_T_API 383 /** 384 * This class is designed to support insertion operations 385 * in the message argument to the LOG4CXX_INFO and similar 386 * macros and is not designed for general purpose use. 387 */ 388 class LOG4CXX_EXPORT WideMessageBuffer 389 { 390 public: 391 /** 392 * Creates a new instance. 393 */ 394 WideMessageBuffer(); 395 /** 396 * Destructor. 397 */ 398 ~WideMessageBuffer(); 399 400 401 /** 402 * Appends string to buffer. 403 * @param msg string append. 404 * @return this buffer. 405 */ 406 WideMessageBuffer& operator<<(const std::basic_string<wchar_t>& msg); 407 /** 408 * Appends string to buffer. 409 * @param msg string to append. 410 * @return this buffer. 411 */ 412 WideMessageBuffer& operator<<(const wchar_t* msg); 413 /** 414 * Appends string to buffer. 415 * @param msg string to append. 416 * @return this buffer. 417 */ 418 WideMessageBuffer& operator<<(wchar_t* msg); 419 420 /** 421 * Appends character to buffer. 422 * @param msg character to append. 423 * @return this buffer. 424 */ 425 WideMessageBuffer& operator<<(const wchar_t msg); 426 427 /** 428 * Insertion operator for STL manipulators such as std::fixed. 429 * @param manip manipulator. 430 * @return encapsulated STL stream. 431 */ 432 std::basic_ostream<wchar_t>& operator<<(ios_base_manip manip); 433 /** 434 * Insertion operator for built-in type. 435 * @param val build in type. 436 * @return encapsulated STL stream. 437 */ 438 std::basic_ostream<wchar_t>& operator<<(bool val); 439 440 /** 441 * Insertion operator for built-in type. 442 * @param val build in type. 443 * @return encapsulated STL stream. 444 */ 445 std::basic_ostream<wchar_t>& operator<<(short val); 446 /** 447 * Insertion operator for built-in type. 448 * @param val build in type. 449 * @return encapsulated STL stream. 450 */ 451 std::basic_ostream<wchar_t>& operator<<(int val); 452 /** 453 * Insertion operator for built-in type. 454 * @param val build in type. 455 * @return encapsulated STL stream. 456 */ 457 std::basic_ostream<wchar_t>& operator<<(unsigned int val); 458 /** 459 * Insertion operator for built-in type. 460 * @param val build in type. 461 * @return encapsulated STL stream. 462 */ 463 std::basic_ostream<wchar_t>& operator<<(long val); 464 /** 465 * Insertion operator for built-in type. 466 * @param val build in type. 467 * @return encapsulated STL stream. 468 */ 469 std::basic_ostream<wchar_t>& operator<<(unsigned long val); 470 /** 471 * Insertion operator for built-in type. 472 * @param val build in type. 473 * @return encapsulated STL stream. 474 */ 475 std::basic_ostream<wchar_t>& operator<<(float val); 476 /** 477 * Insertion operator for built-in type. 478 * @param val build in type. 479 * @return encapsulated STL stream. 480 */ 481 std::basic_ostream<wchar_t>& operator<<(double val); 482 /** 483 * Insertion operator for built-in type. 484 * @param val build in type. 485 * @return encapsulated STL stream. 486 */ 487 std::basic_ostream<wchar_t>& operator<<(long double val); 488 /** 489 * Insertion operator for built-in type. 490 * @param val build in type. 491 * @return encapsulated STL stream. 492 */ 493 std::basic_ostream<wchar_t>& operator<<(void* val); 494 495 496 /** 497 * Cast to ostream. 498 */ 499 operator std::basic_ostream<wchar_t>& (); 500 501 /** 502 * Get content of buffer. 503 * @param os used only to signal that 504 * the embedded stream was used. 505 */ 506 const std::basic_string<wchar_t>& str(std::basic_ostream<wchar_t>& os); 507 508 /** 509 * Get content of buffer. 510 * @param buf used only to signal that 511 * the embedded stream was not used. 512 */ 513 const std::basic_string<wchar_t>& str(WideMessageBuffer& buf); 514 515 /** 516 * Returns true if buffer has an encapsulated STL stream. 517 * @return true if STL stream was created. 518 */ 519 bool hasStream() const; 520 521 private: 522 /** 523 * Prevent use of default copy constructor. 524 */ 525 WideMessageBuffer(const WideMessageBuffer&); 526 /** 527 * Prevent use of default assignment operator. 528 */ 529 WideMessageBuffer& operator=(const WideMessageBuffer&); 530 531 /** 532 * Encapsulated std::string. 533 */ 534 std::basic_string<wchar_t> buf; 535 /** 536 * Encapsulated stream, created on demand. 537 */ 538 std::basic_ostringstream<wchar_t>* stream; 539 }; 540 541 template<class V> 542 std::basic_ostream<wchar_t>& operator<<(WideMessageBuffer& os, const V& val) 543 { 544 return ((std::basic_ostream<wchar_t>&) os) << val; 545 } 546 547 /** 548 * This class is used by the LOG4CXX_INFO and similar 549 * macros to support insertion operators in the message parameter. 550 * The class is not intended for use outside of that context. 551 */ 552 class LOG4CXX_EXPORT MessageBuffer 553 { 554 public: 555 /** 556 * Creates a new instance. 557 */ 558 MessageBuffer(); 559 /** 560 * Destructor. 561 */ 562 ~MessageBuffer(); 563 564 /** 565 * Cast to ostream. 566 */ 567 operator std::ostream& (); 568 569 /** 570 * Appends a string into the buffer and 571 * fixes the buffer to use char characters. 572 * @param msg message to append. 573 * @return encapsulated CharMessageBuffer. 574 */ 575 CharMessageBuffer& operator<<(const std::string& msg); 576 /** 577 * Appends a string into the buffer and 578 * fixes the buffer to use char characters. 579 * @param msg message to append. 580 * @return encapsulated CharMessageBuffer. 581 */ 582 CharMessageBuffer& operator<<(const char* msg); 583 /** 584 * Appends a string into the buffer and 585 * fixes the buffer to use char characters. 586 * @param msg message to append. 587 * @return encapsulated CharMessageBuffer. 588 */ 589 CharMessageBuffer& operator<<(char* msg); 590 591 /** 592 * Appends a string into the buffer and 593 * fixes the buffer to use char characters. 594 * @param msg message to append. 595 * @return encapsulated CharMessageBuffer. 596 */ 597 CharMessageBuffer& operator<<(const char msg); 598 599 /** 600 * Get content of buffer. 601 * @param buf used only to signal 602 * the character type and that 603 * the embedded stream was not used. 604 */ 605 const std::string& str(CharMessageBuffer& buf); 606 607 /** 608 * Get content of buffer. 609 * @param os used only to signal 610 * the character type and that 611 * the embedded stream was used. 612 */ 613 const std::string& str(std::ostream& os); 614 615 /** 616 * Appends a string into the buffer and 617 * fixes the buffer to use char characters. 618 * @param msg message to append. 619 * @return encapsulated CharMessageBuffer. 620 */ 621 WideMessageBuffer& operator<<(const std::wstring& msg); 622 /** 623 * Appends a string into the buffer and 624 * fixes the buffer to use char characters. 625 * @param msg message to append. 626 * @return encapsulated CharMessageBuffer. 627 */ 628 WideMessageBuffer& operator<<(const wchar_t* msg); 629 /** 630 * Appends a string into the buffer and 631 * fixes the buffer to use char characters. 632 * @param msg message to append. 633 * @return encapsulated CharMessageBuffer. 634 */ 635 WideMessageBuffer& operator<<(wchar_t* msg); 636 /** 637 * Appends a string into the buffer and 638 * fixes the buffer to use char characters. 639 * @param msg message to append. 640 * @return encapsulated CharMessageBuffer. 641 */ 642 WideMessageBuffer& operator<<(const wchar_t msg); 643 644 #if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API 645 /** 646 * Appends a string into the buffer and 647 * fixes the buffer to use char characters. 648 * @param msg message to append. 649 * @return encapsulated CharMessageBuffer. 650 */ 651 UniCharMessageBuffer& operator<<(const std::basic_string<UniChar>& msg); 652 /** 653 * Appends a string into the buffer and 654 * fixes the buffer to use char characters. 655 * @param msg message to append. 656 * @return encapsulated CharMessageBuffer. 657 */ 658 UniCharMessageBuffer& operator<<(const UniChar* msg); 659 /** 660 * Appends a string into the buffer and 661 * fixes the buffer to use char characters. 662 * @param msg message to append. 663 * @return encapsulated CharMessageBuffer. 664 */ 665 UniCharMessageBuffer& operator<<(UniChar* msg); 666 /** 667 * Appends a string into the buffer and 668 * fixes the buffer to use char characters. 669 * @param msg message to append. 670 * @return encapsulated CharMessageBuffer. 671 */ 672 UniCharMessageBuffer& operator<<(const UniChar msg); 673 #endif 674 675 #if LOG4CXX_CFSTRING_API 676 /** 677 * Appends a string into the buffer and 678 * fixes the buffer to use char characters. 679 * @param msg message to append. 680 * @return encapsulated CharMessageBuffer. 681 */ 682 UniCharMessageBuffer& operator<<(const CFStringRef& msg); 683 #endif 684 685 /** 686 * Insertion operator for STL manipulators such as std::fixed. 687 * @param manip manipulator. 688 * @return encapsulated STL stream. 689 */ 690 std::ostream& operator<<(ios_base_manip manip); 691 692 /** 693 * Insertion operator for built-in type. 694 * @param val build in type. 695 * @return encapsulated STL stream. 696 */ 697 std::ostream& operator<<(bool val); 698 699 /** 700 * Insertion operator for built-in type. 701 * @param val build in type. 702 * @return encapsulated STL stream. 703 */ 704 std::ostream& operator<<(short val); 705 /** 706 * Insertion operator for built-in type. 707 * @param val build in type. 708 * @return encapsulated STL stream. 709 */ 710 std::ostream& operator<<(int val); 711 /** 712 * Insertion operator for built-in type. 713 * @param val build in type. 714 * @return encapsulated STL stream. 715 */ 716 std::ostream& operator<<(unsigned int val); 717 /** 718 * Insertion operator for built-in type. 719 * @param val build in type. 720 * @return encapsulated STL stream. 721 */ 722 std::ostream& operator<<(long val); 723 /** 724 * Insertion operator for built-in type. 725 * @param val build in type. 726 * @return encapsulated STL stream. 727 */ 728 std::ostream& operator<<(unsigned long val); 729 /** 730 * Insertion operator for built-in type. 731 * @param val build in type. 732 * @return encapsulated STL stream. 733 */ 734 std::ostream& operator<<(float val); 735 /** 736 * Insertion operator for built-in type. 737 * @param val build in type. 738 * @return encapsulated STL stream. 739 */ 740 std::ostream& operator<<(double val); 741 /** 742 * Insertion operator for built-in type. 743 * @param val build in type. 744 * @return encapsulated STL stream. 745 */ 746 std::ostream& operator<<(long double val); 747 /** 748 * Insertion operator for built-in type. 749 * @param val build in type. 750 * @return encapsulated STL stream. 751 */ 752 std::ostream& operator<<(void* val); 753 /** 754 * Get content of buffer. 755 * @param buf used only to signal 756 * the character type and that 757 * the embedded stream was not used. 758 */ 759 const std::wstring& str(WideMessageBuffer& buf); 760 761 /** 762 * Get content of buffer. 763 * @param os used only to signal 764 * the character type and that 765 * the embedded stream was used. 766 */ 767 const std::wstring& str(std::basic_ostream<wchar_t>& os); 768 769 #if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API 770 /** 771 * Get content of buffer. 772 * @param buf used only to signal 773 * the character type and that 774 * the embedded stream was not used. 775 */ 776 const std::basic_string<UniChar>& str(UniCharMessageBuffer& buf); 777 778 /** 779 * Get content of buffer. 780 * @param os used only to signal 781 * the character type and that 782 * the embedded stream was used. 783 */ 784 const std::basic_string<UniChar>& str(UniCharMessageBuffer::uostream& os); 785 #endif 786 787 /** 788 * Returns true if buffer has an encapsulated STL stream. 789 * @return true if STL stream was created. 790 */ 791 bool hasStream() const; 792 793 private: 794 /** 795 * Prevent use of default copy constructor. 796 */ 797 MessageBuffer(const MessageBuffer&); 798 /** 799 * Prevent use of default assignment operator. 800 */ 801 MessageBuffer& operator=(const MessageBuffer&); 802 803 /** 804 * Character message buffer. 805 */ 806 CharMessageBuffer cbuf; 807 808 /** 809 * Encapsulated wide message buffer, created on demand. 810 */ 811 WideMessageBuffer* wbuf; 812 #if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API 813 /** 814 * Encapsulated wide message buffer, created on demand. 815 */ 816 UniCharMessageBuffer* ubuf; 817 #endif 818 }; 819 820 template<class V> 821 std::ostream& operator<<(MessageBuffer& os, const V& val) 822 { 823 return ((std::ostream&) os) << val; 824 } 825 826 #if LOG4CXX_LOGCHAR_IS_UTF8 827 typedef CharMessageBuffer LogCharMessageBuffer; 828 #endif 829 830 #if LOG4CXX_LOGCHAR_IS_WCHAR 831 typedef WideMessageBuffer LogCharMessageBuffer; 832 #endif 833 834 #if LOG4CXX_LOGCHAR_IS_UNICHAR 835 typedef UniCharMessageBuffer LogCharMessageBuffer; 836 #endif 837 838 #else 839 typedef CharMessageBuffer MessageBuffer; 840 typedef CharMessageBuffer LogCharMessageBuffer; 841 #endif 842 843 } 844 } 845 846 #if defined(_MSC_VER) 847 #pragma warning (pop) 848 #endif 849 850 #endif 851 852