17. Log Message Timestamp

#include <iostream>
#include <string>
using namespace std;

// includeTimestamp default = false
void logMessage(const string& msg, bool includeTimestamp = false) {
    if (includeTimestamp) {
        cout << "[123456] " << msg;
    } else {
        cout << msg;
    }
}

int main() {
    int mode;
    string msg;
    cin >> mode >> msg;

    if (mode == 0) {
        logMessage(msg);            // default: no timestamp
    } else {
        logMessage(msg, true);      // timestamp enabled
    }

    return 0;
}

Explanation & Logic Summary:

  • Default argument includeTimestamp = false makes the timestamp optional.
  • Logging behavior changes based on the boolean flag.
  • Adding optional timestamp support does not require a second overload.
  • The message prints differently depending on whether timestamp is requested.

Firmware Relevance & Real-World Context:

  • Logging systems in embedded firmware often support optional timestamps for debugging.
  • Default arguments simplify API calls when timestamps are not needed.
  • Mirrors real-world logging frameworks in RTOS, HAL, and diagnostic modules.
  • Keeps the API minimal while still supporting richer output when required.

 

 

 

Loading...

Input

0 Hello

Expected Output

Hello