pistacheio / pistache

A high-performance REST toolkit written in C++
https://pistacheio.github.io/pistache/
Apache License 2.0
3.12k stars 690 forks source link

Boost logging compile error #1081

Closed xinthose closed 2 years ago

xinthose commented 2 years ago

I get errors when compiling pistache with my global boost logger. My OS is Ubuntu 22. I never used to have these issues on Ubuntu 20. I am using the latest version of pistache. I am not sure how to fix this.

make ccache g++ -Wall -std=c++17 -O3 -DBOOST_LOG_DYN_LINK -DBOOST_BIND_GLOBAL_PLACEHOLDERS -m64 -DPISTACHE_USED -DARD_SERIAL_USED -c src/main.cpp -o obj/main.o In file included from /usr/include/boost/log/trivial.hpp:24, from src/../include/logging.hpp:12, from src/../include/main.hpp:34, from src/main.cpp:6: src/../include/logging.hpp:16:15: error: expected identifier before ‘for’ 16 #define TRACE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::trace) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:16:15: error: expected ‘}’ before ‘for’ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:22:22: note: to match this ‘{’ 22 enum class Level { ^ In file included from /usr/include/boost/log/trivial.hpp:24, from src/../include/logging.hpp:12, from src/../include/main.hpp:34, from src/main.cpp:6: src/../include/logging.hpp:16:15: error: expected unqualified-id before ‘for’ 16 #define TRACE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::trace) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:16:15: error: expected unqualified-id before ‘!’ token 16 #define TRACE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::trace) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:16:15: error: expected unqualified-id before ‘)’ token 16 #define TRACE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::trace) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:17:15: error: expected unqualified-id before ‘!’ token 17 #define DEBUG BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::debug) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:17:15: error: expected unqualified-id before ‘)’ token 17 #define DEBUG BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::debug) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:18:14: error: expected unqualified-id before ‘!’ token 18 #define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:18:14: error: expected unqualified-id before ‘)’ token 18 #define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:20:15: error: expected unqualified-id before ‘!’ token 20 #define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:20:15: error: expected unqualified-id before ‘)’ token 20 #define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:21:15: error: expected unqualified-id before ‘!’ token 21 #define FATAL BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::fatal) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ src/../include/logging.hpp:21:15: error: expected unqualified-id before ‘)’ token 21 #define FATAL BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::fatal) << FILENAME << "/" << FUNCTION << ":" << LINE << " >> " ^~~~~ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:34:26: error: ‘Level’ has not been declared 34 virtual void log(Level level, const std::string& message) = 0; ^~~~~ /usr/include/pistache/string_logger.h:35:35: error: ‘Level’ has not been declared 35 virtual bool isEnabledFor(Level level) const = 0; ^~~~~ /usr/include/pistache/string_logger.h:43:44: error: expected ‘)’ before ‘level’ 43 explicit StringToStreamLogger(Level level, std::ostream* out = &std::cerr) ~ ^~ ) /usr/include/pistache/string_logger.h:49:18: error: ‘Level’ has not been declared 49 void log(Level level, const std::string& message) override; ^~~~~ /usr/include/pistache/string_logger.h:50:27: error: ‘Level’ has not been declared 50 bool isEnabledFor(Level level) const override; ^~~~~ /usr/include/pistache/string_logger.h:53:9: error: ‘Level’ does not name a type 53 Level level_; ^~~~~ /usr/include/pistache/string_logger.h:57:1: error: expected declaration before ‘}’ token 57 } // namespace Pistache::Log ^ In file included from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/listener.h:66:19: error: ‘StringLogger’ is not a member of ‘Pistache::Log’; did you mean ‘StringLogger’? 66 PISTACHE_STRING_LOGGER_T logger = PISTACHE_NULL_STRING_LOGGER); ^~~~~~~~ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:31:11: note: ‘StringLogger’ declared here 31 class StringLogger ^~~~ In file included from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/listener.h:66:19: error: template argument 1 is invalid 66 PISTACHE_STRING_LOGGER_T logger = PISTACHE_NULL_STRING_LOGGER); ^~~~~~~~ /usr/include/pistache/listener.h:122:9: error: ‘StringLogger’ is not a member of ‘Pistache::Log’; did you mean ‘StringLogger’? 122 PISTACHE_STRING_LOGGERT logger = PISTACHE_NULL_STRING_LOGGER; ^~~~~~~~ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:31:11: note: ‘StringLogger’ declared here 31 class StringLogger ^~~~ In file included from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/listener.h:122:9: error: template argument 1 is invalid 122 PISTACHE_STRING_LOGGERT logger = PISTACHE_NULL_STRING_LOGGER; ^~~~~~~~ /usr/include/pistache/listener.h:66:53: error: could not convert ‘nullptr’ from ‘std::nullptr_t’ to ‘int’ 66 PISTACHE_STRING_LOGGER_T logger = PISTACHE_NULL_STRING_LOGGER); ^~~~~~~
std::nullptr_t

/usr/include/pistache/listener.h:122:44: error: cannot convert ‘std::nullptr_t’ to ‘int’ in initialization 122 | PISTACHE_STRING_LOGGERT logger = PISTACHE_NULL_STRING_LOGGER; | ^~~~~~~ /usr/include/pistache/endpoint.h:61:29: error: ‘StringLogger’ is not a member of ‘Pistache::Log’; did you mean ‘StringLogger’? 61 | Options& logger(PISTACHE_STRING_LOGGER_T logger); | ^~~~~~~~ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:31:11: note: ‘StringLogger’ declared here 31 | class StringLogger | ^~~~ In file included from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/endpoint.h:61:29: error: template argument 1 is invalid 61 | Options& logger(PISTACHE_STRING_LOGGER_T logger); | ^~~~~~~~ /usr/include/pistache/endpoint.h:84:13: error: ‘StringLogger’ is not a member of ‘Pistache::Log’; did you mean ‘StringLogger’? 84 | PISTACHE_STRING_LOGGERT logger; | ^~~~~~~~ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:31:11: note: ‘StringLogger’ declared here 31 | class StringLogger | ^~~~ In file included from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/endpoint.h:84:13: error: template argument 1 is invalid 84 | PISTACHE_STRING_LOGGERT logger; | ^~~~~~~~ /usr/include/pistache/endpoint.h:204:9: error: ‘StringLogger’ is not a member of ‘Pistache::Log’; did you mean ‘StringLogger’? 204 | PISTACHE_STRING_LOGGERT logger = PISTACHE_NULL_STRING_LOGGER; | ^~~~~~~~ In file included from /usr/include/pistache/log.h:15, from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/string_logger.h:31:11: note: ‘StringLogger’ declared here 31 | class StringLogger | ^~~~ In file included from /usr/include/pistache/listener.h:18, from /usr/include/pistache/endpoint.h:16, from src/../include/pistache.hpp:27, from src/../include/main.hpp:42, from src/main.cpp:6: /usr/include/pistache/endpoint.h:204:9: error: template argument 1 is invalid 204 | PISTACHE_STRING_LOGGERT logger = PISTACHE_NULL_STRING_LOGGER; | ^~~~~~~~ /usr/include/pistache/endpoint.h:204:44: error: cannot convert ‘std::nullptr_t’ to ‘int’ in initialization 204 | PISTACHE_STRING_LOGGERT logger = PISTACHE_NULL_STRING_LOGGER; | ^~~~~~~ make: *** [Makefile:110: obj/main.o] Error 1

logging.hpp

#ifndef LOGGING_HPP
#define LOGGING_HPP

#include <boost/log/expressions.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>

#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
#define TRACE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::trace) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "
#define DEBUG BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::debug) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "
#define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "
#define WARNING BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::warning) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "
#define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "
#define FATAL BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::fatal) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "

#define SYS_LOGFILE "/var/www/html/logs/pal_%y-%m-%d_#%N.log"

//Narrow-char thread-safe logger.
typedef boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger_t;

//declares a global logger with a custom initialization
BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)

#endif
g++ --version
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
xinthose commented 2 years ago

I figured out the issue. I had to change all of my logging macros to a different name (DEBUG1, INFO1, WARNING1, ERROR1, FATAL1). Does pistache use these names in its logging then? (DEBUG, INFO, WARNING, ERROR, FATAL). To avoid future issues, you should probably use unique names likeP_INFO in the library.

Tachi107 commented 2 years ago

Does pistache use these names in its logging then?

No, Pistache does not define those macros. It only defines TRACE, DEBUG, etc in an enum class named Level, in namespace Pistache::Log. Are you sure that Pistache is the one causing your issue?

https://github.com/pistacheio/pistache/blob/615281a33eccd9c1d09f274254fd28313b0cd582/include/pistache/string_logger.h#L22-L29

dennisjenkins75 commented 2 years ago

A trick that you can do is add -DDEBUG=@@@ to your compiler args. This will cause the first (re)definition of DEBUG to fail, and make it easier to find.

xinthose commented 2 years ago

@Tachi107 yes, because my code compiles fine without it. @dennisjenkins75 I just tried compiling with that flag and got that DEBUG is redefined in my header file

src/../include/logging.hpp:17: warning: "DEBUG" redefined
   17 | #define DEBUG BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::debug) << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << " >> "
Tachi107 commented 2 years ago

@Tachi107 yes, because my code compiles fine without it.

Does your code compile with Pistache and without the Boost library?

xinthose commented 2 years ago

@Tachi107 I cannot test that because boost and global logging is an integral part of my program. I am fine with using a different macro name, just glad I found the issue.

Tachi107 commented 2 years ago

It's hard (for me) to figure out what issue are you encountering... Could you provide a minimal example that is reproducible for us?

xinthose commented 2 years ago

@Tachi107 the issue is that my DEBUG define is conflicting with yours https://github.com/pistacheio/pistache/blob/615281a33eccd9c1d09f274254fd28313b0cd582/include/pistache/string_logger.h#L24

Tachi107 commented 2 years ago

Oh, now I got it... Pistache is not creating issues in your code, it's your code that is creating issues in Pistache :)

Pistache defines the identifier DEBUG in an enum class, inside a nested namespace. This means that Pistache's DEBUG won't ever pollute user's global namespace (unless you do something like using Pistache::Log::Level::DEBUG).

Now, when you define DEBUG as a macro in your code, the word DEBUG in Pistache's headers get replaced with the expanded macro, and that's obviously going to cause issues.

This isn't really a bug in Pistache IMO, it's a bit like doing #define log my_special_function() and expecting that everything will keep working fine.

Closing this, but if you think that I came to the wrong conclusion please let me know :)

xinthose commented 2 years ago

@Tachi107 yah, thanks. At least this is documented here now in case anyone ever searches for it.