1+ // *******************************************************************************
2+ // Copyright (c) 2026 Contributors to the Eclipse Foundation
3+ //
4+ // See the NOTICE file(s) distributed with this work for additional
5+ // information regarding copyright ownership.
6+ //
7+ // This program and the accompanying materials are made available under the
8+ // terms of the Apache License Version 2.0 which is available at
9+ // <https://www.apache.org/licenses/LICENSE-2.0>
10+ //
11+ // SPDX-License-Identifier: Apache-2.0
12+ // *******************************************************************************
13+
14+ #include " score/mw/log/logger.h"
15+
16+ #include < chrono>
17+ #include < cstdlib>
18+ #include < iostream>
19+ #include < string_view>
20+ #include < thread>
21+
22+ using namespace std ::chrono_literals;
23+
24+ namespace integration
25+ {
26+
27+ namespace config
28+ {
29+ // logging routing contexts
30+ constexpr std::string_view kSensorContext {" SENSOR" };
31+ constexpr std::string_view kControlContext {" CTRL" };
32+ } // namespace config
33+
34+ class SensorPipeline
35+ {
36+ public:
37+ SensorPipeline ()
38+ : logger_{score::mw::log::CreateLogger (
39+ config::kSensorContext .data (),
40+ " Sensor" )}
41+ {
42+ // log info: sensor service startup
43+ logger_.LogInfo ()
44+ << " sensor initialized" ;
45+ }
46+
47+ void Process (std::uint32_t frame_id)
48+ {
49+ // log debug: frame entry tracking
50+ logger_.LogDebug ()
51+ << " frame received=" << frame_id;
52+
53+ // log info: frame processed successfully
54+ logger_.LogInfo ()
55+ << " frame processed"
56+ << " id=" << frame_id;
57+
58+ if (frame_id == 3U )
59+ {
60+ // log warn : processing delay detected
61+ logger_.LogWarn ()
62+ << " processing delay frame=" << frame_id;
63+ }
64+
65+ if (frame_id == 5U )
66+ {
67+ // log error : frame processing failure
68+ logger_.LogError ()
69+ << " processing failed frame=" << frame_id;
70+ }
71+ }
72+
73+ private:
74+ score::mw::log::Logger &logger_;
75+ };
76+
77+ class VehicleController
78+ {
79+ public:
80+ VehicleController ()
81+ : logger_{score::mw::log::CreateLogger (
82+ config::kControlContext .data (),
83+ " Control" )}
84+ {
85+ // log info : controller initialization complete
86+ logger_.LogInfo ()
87+ << " controller ready mode=AUTO" ;
88+ }
89+
90+ void Execute (std::uint32_t frame_id)
91+ {
92+ const std::uint32_t speed{frame_id * 10U };
93+
94+ // log debug : control cycle execution trace
95+ logger_.LogDebug ()
96+ << " control cycle=" << frame_id;
97+
98+ // log info : control decision applied
99+ logger_.LogInfo ()
100+ << " control applied"
101+ << " id=" << frame_id;
102+
103+ if (frame_id >= 6U )
104+ {
105+ // log warn : high speed condition detected
106+ logger_.LogWarn ()
107+ << " high speed frame=" << frame_id;
108+ }
109+
110+ if (frame_id == 7U )
111+ {
112+ // log fatal : emergency stop
113+ logger_.LogFatal ()
114+ << " emergency stop triggered at frame=" << frame_id;
115+ }
116+ }
117+
118+ private:
119+ score::mw::log::Logger &logger_;
120+ };
121+
122+ class Application
123+ {
124+ public:
125+ void Run ()
126+ {
127+ std::cout << " \n Logging Example\n " ;
128+
129+ SensorPipeline sensor;
130+ VehicleController control;
131+
132+ for (std::uint32_t frame{1U }; frame <= 8U ; ++frame)
133+ {
134+ std::cout << " Frame=" << frame << ' \n ' ;
135+
136+ sensor.Process (frame);
137+ control.Execute (frame);
138+
139+ std::this_thread::sleep_for (300ms);
140+ }
141+
142+ std::cout << " \n Logging Example finished\n " ;
143+ }
144+ };
145+
146+ } // namespace integration
147+
148+ int main ()
149+ {
150+ integration::Application app;
151+ app.Run ();
152+ return EXIT_SUCCESS ;
153+ }
0 commit comments