• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   snprintf.hpp
9  * \author Andrey Semashev
10  * \date   20.02.2009
11  *
12  * \brief  This header is the Boost.Log library implementation, see the library documentation
13  *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
14  */
15 
16 #ifndef BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_
18 
19 #include <stdio.h>
20 #include <cstddef>
21 #include <cstdarg>
22 #include <boost/log/detail/config.hpp>
23 #ifdef BOOST_LOG_USE_WCHAR_T
24 #include <wchar.h>
25 #endif // BOOST_LOG_USE_WCHAR_T
26 #include <boost/log/detail/header.hpp>
27 
28 #ifdef BOOST_HAS_PRAGMA_ONCE
29 #pragma once
30 #endif
31 
32 namespace boost {
33 
34 BOOST_LOG_OPEN_NAMESPACE
35 
36 namespace aux {
37 
38 #if defined(_MSC_VER) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
39 
40 // MSVC snprintfs are not conforming but they are good enough for our cases.
41 // MinGW32, at least the older versions up until gcc 4.7, also provide the non-conforming interface.
vsnprintf(char * buf,std::size_t size,const char * format,std::va_list args)42 inline int vsnprintf(char* buf, std::size_t size, const char* format, std::va_list args)
43 {
44     int n = _vsnprintf(buf, size, format, args);
45     if (static_cast< unsigned int >(n) >= size)
46     {
47         n = static_cast< int >(size);
48         buf[size - 1] = '\0';
49     }
50     return n;
51 }
52 
53 #   ifdef BOOST_LOG_USE_WCHAR_T
vswprintf(wchar_t * buf,std::size_t size,const wchar_t * format,std::va_list args)54 inline int vswprintf(wchar_t* buf, std::size_t size, const wchar_t* format, std::va_list args)
55 {
56     int n = _vsnwprintf(buf, size, format, args);
57     if (static_cast< unsigned int >(n) >= size)
58     {
59         n = static_cast< int >(size);
60         buf[size - 1] = L'\0';
61     }
62     return n;
63 }
64 #   endif // BOOST_LOG_USE_WCHAR_T
65 
snprintf(char * buf,std::size_t size,const char * format,...)66 inline int snprintf(char* buf, std::size_t size, const char* format, ...)
67 {
68     std::va_list args;
69     va_start(args, format);
70     int n = vsnprintf(buf, size, format, args);
71     va_end(args);
72     return n;
73 }
74 
75 #   ifdef BOOST_LOG_USE_WCHAR_T
swprintf(wchar_t * buf,std::size_t size,const wchar_t * format,...)76 inline int swprintf(wchar_t* buf, std::size_t size, const wchar_t* format, ...)
77 {
78     std::va_list args;
79     va_start(args, format);
80     int n = vswprintf(buf, size, format, args);
81     va_end(args);
82     return n;
83 }
84 #   endif // BOOST_LOG_USE_WCHAR_T
85 
86 #else
87 
88 // Standard-conforming compilers already have the correct snprintfs
89 using ::snprintf;
90 using ::vsnprintf;
91 
92 #   ifdef BOOST_LOG_USE_WCHAR_T
93 using ::swprintf;
94 using ::vswprintf;
95 #   endif // BOOST_LOG_USE_WCHAR_T
96 
97 #endif
98 
99 } // namespace aux
100 
101 BOOST_LOG_CLOSE_NAMESPACE // namespace log
102 
103 } // namespace boost
104 
105 #include <boost/log/detail/footer.hpp>
106 
107 #endif // BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_
108