Using object from another file
-
you have to include header file with Log definition in every cpp file in which you want to use Log objects.
-
you have to include header file with Log definition in every cpp file in which you want to use Log objects.
@rozpruwacz that would mean I have to call Log.begin in all cpp files as well, right? So if I want to change the log level from VERBOSE to NOTICE, or the log target from Serial to something else, I would have to edit all cpp files?
That seems very inelegant and inefficient to me. But I am not very skilled in c/c++. Maybe I'm just spoiled by other languages that has better ways to handle this?
-
@rozpruwacz that would mean I have to call Log.begin in all cpp files as well, right? So if I want to change the log level from VERBOSE to NOTICE, or the log target from Serial to something else, I would have to edit all cpp files?
That seems very inelegant and inefficient to me. But I am not very skilled in c/c++. Maybe I'm just spoiled by other languages that has better ways to handle this?
@mfalkvidd no. I'm assuming that Log object is declared in some cpp file of the library You use. And there is a header file that has something like this:
extern X Log;
X is the class of the Log object.
then if you include the header file in multiple cpp files, there still will be only one Log object. You just have to make sure that the Log.begin will be called once (in one of the cpp files or ino file) before any other calls to the Log object in other cpp/ino files. -
oh, and maybe the most important, Log is an object not a class :)
-
oh, and maybe the most important, Log is an object not a class :)
-
Where is Log declared?
it is a global and defined in the CPP file for the Logging class:
Logging Log = Logging();so, if it is Global... why the heck can't it be accessed in the Configuration class? Well each set of files is a different translation unit...
Have you tried adding
#include <ArduinoLog.h>in your
"Configuration.h"class?
-
Where is Log declared?
it is a global and defined in the CPP file for the Logging class:
Logging Log = Logging();so, if it is Global... why the heck can't it be accessed in the Configuration class? Well each set of files is a different translation unit...
Have you tried adding
#include <ArduinoLog.h>in your
"Configuration.h"class?
@BulldogLowell I was hoping to avoid including ArduinoLog in all .h files, but there seems to be no way around that.
I guess some sort of dependency injection would work, but dependency injection and embedded code might not work well together.
-
don't include ArduinoLog.h in header files, only in cpp files.
-
@BulldogLowell I was hoping to avoid including ArduinoLog in all .h files, but there seems to be no way around that.
I guess some sort of dependency injection would work, but dependency injection and embedded code might not work well together.
@mfalkvidd said in Using object from another file:
@BulldogLowell I was hoping to avoid including ArduinoLog in all .h files, but there seems to be no way around that.
what's the problem with adding the #include directive? It is a dependency by definition....
-
@mfalkvidd said in Using object from another file:
@BulldogLowell I was hoping to avoid including ArduinoLog in all .h files, but there seems to be no way around that.
what's the problem with adding the #include directive? It is a dependency by definition....