126. Runtime Logging Abstraction

Firmware frequently sends diagnostic and debug messages to external systems.
Depending on system configuration or hardware availability, the same firmware image may need to transmit logs over different communication transports such as UART or SPI.

The logging logic must not depend on transport-specific details like registers, flags, or framing rules.
Instead, logging must be performed through a runtime-selected stream abstraction, allowing the transport to be changed without modifying application logic.

Your task is to complete the design so that log messages are written through an abstract output stream selected at runtime.
The application logic must remain completely independent of the chosen transport.

Program Flow:

  1. The program reads an integer T representing the transport type.
  2. The program reads an integer N, representing the number of log messages.
  3. The program reads N log messages sequentially.
  4. Each message is written to the selected communication stream.
  5. Each transmitted message is printed with a transport-specific prefix.

Input Format:

Input is provided via standard input (stdin).

  • First value:
    T (integer)
    • 1 → UART transport
    • 2 → SPI transport
  • Second value:
    N (integer)
    • Range: 1 ≤ N ≤ 10
  • Next N values:
    • One string per line
    • Maximum length: 32 characters
    • No spaces inside a message

Output Format:

  • Print exactly N lines.
  • Each line must be exactly one of the following formats:

For UART:

UART:<message> 

For SPI:

SPI:<message> 

Rules:

  • Output order must match input order
  • No extra spaces or additional text
  • Transport selection must occur at runtime
  • Logging logic must not contain transport-specific code
  • Transport-specific behavior must be isolated behind an abstraction
  • Invalid transport selection must result in no output
  • main() must not be modified
  • No dynamic memory allocation
  • No STL containers
  • Deterministic behavior is required

Example:

Example 1

Input:

1 3
SystemOK
TempHigh
Reset 

Output:

UART:SystemOK
UART:TempHigh
UART:Reset 

Example 2

Input:

2
2
Ping
Pong

Output:

SPI:Ping
SPI:Pong

Constraints:

  • Maximum message length is fixed at compile time
  • Transport objects must have static or automatic lifetime
  • Code must be suitable for embedded / firmware environments

 

 

 

Loading...

Input

1 2 Hello World

Expected Output

UART:Hello UART:World