161. 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

 

 

 

Need Help? Refer to the Quick Guide below

Abstraction is the process of exposing only the essential features of an object while hiding the complex implementation details ("the wiring") from the user.

Think of a Car:

  • Abstraction (Interface): Steering wheel, pedals, gear stick. (What the user sees).
  • Implementation (Hidden Details): Fuel injection timing, combustion cycles, differential gears. (What happens inside).

In C++, we achieve this using Access Specifiers (public/private) and Abstract Classes (Interfaces).

Syntax & Usage

1. Data Abstraction (The Public API)

Designing a class where the user sees simple functions, but the complex logic happens privately.

class WiFiModule {
private:
    // Complex hidden details (User doesn't need to see these)
    void spi_write(uint8_t byte) { /* ... */ }
    void handshake_tcp() { /* ... */ }
    int socket_id;

public:
    // Simple Abstraction (User sees only this)
    void connect(const char* ssid, const char* pass) {
        spi_write(0x01); // Internal logic
        handshake_tcp(); // Internal logic
    }
};

int main() {
    WiFiModule wifi;
    // The user calls one simple function.
    // They don't know (or care) that it triggered 50 SPI transactions.
    wifi.connect("HomeNet", "1234");
}

2. Abstract Classes (Pure Interfaces)

Defining a blueprint that enforces what a device must do, without defining how.

// Abstract Base Class
class IMotor {
public:
    virtual void setSpeed(int speed) = 0; // Pure Virtual
    virtual void stop() = 0;
};

// The user code works with the "IMotor" abstraction,
// ignoring whether it's a DC Motor or Stepper Motor.
void emergency_shutdown(IMotor* m) {
    m->stop();
}

Abstraction vs. Encapsulation

These two are often confused but are distinct.

FeatureEncapsulationAbstraction
FocusInformation Hiding.Implementation Hiding.
GoalProtect data from external corruption.Reduce complexity for the user.
MechanismGetters/Setters, private variables.Interfaces, Abstract Classes.
AnalogyThe plastic casing around a wire.The simple "On/Off" switch.

Relevance in Embedded/Firmware

1. HAL (Hardware Abstraction Layer)

This is the textbook definition of abstraction in firmware.

You write code like GPIO_Write(PIN_5, HIGH).

  • Abstraction: "Set Pin 5 High".
  • Implementation: On AVR, this writes to PORTB. On STM32, it writes to BSRR register. On Linux, it writes to a file /sys/class/gpio. Your application logic relies on the abstraction, making it portable.

2. Reducing Cognitive Load

A Junior Developer can use a complex driver (e.g., a FAT32 filesystem wrapper) by just calling file.open() and file.write(). They don't need to understand sectors, clusters, or allocation tables to use it effectively.

Common Pitfalls (Practical Tips)

PitfallDetails
❌ Leaky Abstractions

When implementation details "leak" out.

Example: A generic Motor class having a function setStepperMicrosteps(). This breaks the abstraction because not all motors are steppers.

❌ Over-AbstractionCreating wrappers around wrappers (Driver -> Hal -> LL -> Register). Too many layers add overhead and make debugging harder ("Spaghetti Code"). Keep it flat where possible.
✅ Design from the User's ViewWhen writing a class, write the main() code first (how you want to use it). Then implement the class to match that simple API.