#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h> // For Sleep

void logMessage(FILE *logFile, const char *message) {
    if (logFile != NULL) {
        fprintf(logFile, "%s\n", message);
        fflush(logFile); // Ensure the message is written immediately
    }
}

char* getCurrentTime() {
    time_t rawTime;
    struct tm *timeInfo;
    static char timeBuffer[20];

    time(&rawTime);
    timeInfo = localtime(&rawTime);

    strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%dT%H:%M:%S", timeInfo);
    return timeBuffer;
}

int main() {
    FILE *logFile = fopen("execution_log.txt", "a");
    if (logFile == NULL) {
        printf("Error opening log file.\n");
        return 1;
    }

    char buffer[100];

    // Log execution start time
    snprintf(buffer, sizeof(buffer), "Execution started at: %s", getCurrentTime());
    logMessage(logFile, buffer);

    logMessage(logFile, "Logging execution progress...");

    // Simulate some work
    for (int i = 0; i < 5; i++) {
        snprintf(buffer, sizeof(buffer), "Step %d completed", i + 1);
        logMessage(logFile, buffer);
        Sleep(1000); // Sleep for 1 second
    }

    logMessage(logFile, "Execution completed successfully.");

    // Close the log file
    fclose(logFile);

    return 0;
}
