v0.15.0
Loading...
Searching...
No Matches
log.cpp
Go to the documentation of this file.
1/**
2 * @file log.cpp
3 * @example log.cpp
4 * @brief Example and test how to log
5 *
6 * This is an example of how to use the logger.
7 *
8 */
9
10#include <MoFEM.hpp>
11
12#include <thread>
13#include <chrono>
14
15using namespace MoFEM;
16
19
20 MOFEM_LOG_CHANNEL("WORLD");
21 // Log time
22 BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
23 // Log tag
24 MOFEM_LOG_TAG("WORLD", "Tag this output");
25
26 MOFEM_LOG("WORLD", Sev::verbose) << "Hello, world!";
27
28 // sleep for half a second
29 std::this_thread::sleep_for(std::chrono::milliseconds(300));
30
31 MOFEM_LOG("WORLD", Sev::verbose) << "Hello, second time world!";
32
34}
35
39
40 // Log scope
41 MOFEM_LOG("SYNC", Sev::verbose) << "Hello, sync!";
42 MOFEM_LOG("SYNC", Sev::verbose) << "Hello again, sync!";
43
45}
46
49
50 // Log scope
52 MOFEM_LOG_CHANNEL("SYNC");
54 MOFEM_LOG("SYNC", Sev::verbose) << "Hello, sync!";
55 MOFEM_LOG("SYNC", Sev::verbose) << "Hello again, sync!";
56
58
60}
61
62static char help[] = "...\n\n";
63
64int main(int argc, char *argv[]) {
65
66 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
67
68 try {
69
70 moab::Core mb_instance;
71 moab::Interface &moab = mb_instance;
72
73 MoFEM::Core core(moab, PETSC_COMM_WORLD);
74 MoFEM::Interface &m_field = core;
75
76 CHKERR PetscPrintf(PETSC_COMM_WORLD,
77 "\nTesting logging for obsolete way of printing "
78 "messages\nnext line\nnext line\n\n");
79
80 CHKERR PetscPrintf(PETSC_COMM_WORLD, "Ala have ");
81 CHKERR PetscPrintf(PETSC_COMM_WORLD, "a ");
82 CHKERR PetscPrintf(PETSC_COMM_WORLD, "cat\n");
83
84 CHKERR PetscPrintf(PETSC_COMM_WORLD, "WARNING\n");
85
86 // Set "WORLD channel"
87 MOFEM_LOG_CHANNEL("WORLD");
88 {
89 MOFEM_LOG("WORLD", Sev::error) << "Hello, self error!";
90 MOFEM_LOG("WORLD", Sev::warning) << "Hello, self warning!";
91 MOFEM_LOG("WORLD", Sev::inform) << "Hello, self inform!";
92 MOFEM_LOG("WORLD", Sev::verbose) << "Hello, self verbose!";
93 MOFEM_LOG("WORLD", Sev::noisy) << "Hello, self noisy!";
94 }
95
96 {
97 MOFEM_LOG_C("WORLD", Sev::inform, "%s %d %d %d", "Hello C, self error!",
98 1, 2, 3);
99 }
100
101 {
105 }
106
107 // Create channel
108 auto core_log = logging::core::get();
109 core_log->add_sink(
111 LogManager::setLog("ATOM_TEST");
112
113 // add sink to channel
114 core_log->add_sink(LogManager::createSink(
115 boost::make_shared<std::ofstream>("log0.log"), "ATOM_TEST"));
116
117 // add sink to channel other way
118 logging::add_file_log(keywords::file_name = "log1.log",
119 keywords::filter =
120 MoFEM::LogKeywords::channel == "ATOM_TEST");
121
122 // add skink to channel third way
123 auto backend = boost::make_shared<sinks::text_ostream_backend>();
124 backend->add_stream(boost::make_shared<std::ofstream>("log2.log"));
125 auto sink = boost::make_shared<LogManager::SinkType>(backend);
126 sink->set_filter((expr::has_attr(MoFEM::LogKeywords::channel) &&
127 MoFEM::LogKeywords::channel == "ATOM_TEST"));
128 core_log->add_sink(sink);
129
130 MOFEM_LOG_TAG("ATOM_TEST", "atom test");
131 MOFEM_LOG("ATOM_TEST", Sev::inform) << "Test atom test channel";
132
133 FILE *file = std::fopen("log.txt", "w");
134 if (file == nullptr) {
135 SETERRQ(PETSC_COMM_WORLD, MOFEM_OPERATION_UNSUCCESSFUL,
136 "Failed to open file for logging");
137 }
138
139 core_log->add_sink(LogManager::createSink(
140 LogManager::getStrmSync(m_field.get_comm(), file), "WRITE_TEST"));
141 LogManager::setLog("WRITE_TEST");
142
143 MOFEM_LOG("WRITE_TEST", Sev::inform) << "Writing to file";
144
145 MOFEM_LOG_FILE_SYNCHRONISE(m_field.get_comm(), file);
146
147 // Always close the file
148 std::fclose(file);
149
150 // SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY, "Trigger error");
151 }
153
155}
#define MOFEM_LOG_FILE_SYNCHRONISE(comm, fd)
Synchronise "SYNC" channel.
#define MOFEM_LOG_SYNCHRONISE(comm)
Synchronise "SYNC" channel.
#define MOFEM_LOG_C(channel, severity, format,...)
int main()
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
#define MOFEM_LOG_ATTRIBUTES(channel, bit)
Add attributes to channel.
#define MOFEM_LOG_FUNCTION()
Set scope.
static char help[]
Definition log.cpp:62
MoFEMErrorCode log_fun1()
Definition log.cpp:17
MoFEMErrorCode log_fun3()
Definition log.cpp:36
MoFEMErrorCode log_fun2()
Definition log.cpp:47
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
virtual MPI_Comm & get_comm() const =0
Core (interface) class.
Definition Core.hpp:82
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:72
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:118
Deprecated interface functions.
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
static boost::shared_ptr< std::ostream > getStrmSync()
Get the strm sync object.
static boost::shared_ptr< std::ostream > getStrmSelf()
Get the strm self object.