Logging in AngelScript

Here’s the translation of the Go logging example to AngelScript, formatted in Markdown for Hugo:

import std.string;
import std.filesystem;

void main() {
    // Simple logging using print statements
    print("standard logger\n");

    // Simulating timestamp with microsecond accuracy
    string timestamp = getTimestamp();
    print(timestamp + " with micro\n");

    // Simulating file and line information
    print(timestamp + " " + __FILE__ + ":" + __LINE__ + ": with file/line\n");

    // Custom logger with prefix
    void customLog(const string &in message) {
        print("my:" + getTimestamp() + " " + message + "\n");
    }
    customLog("from mylog");

    // Changing prefix
    void customLogWithNewPrefix(const string &in message) {
        print("ohmy:" + getTimestamp() + " " + message + "\n");
    }
    customLogWithNewPrefix("from mylog");

    // Logging to a string
    string logBuffer;
    void bufferLog(const string &in message) {
        logBuffer += "buf:" + getTimestamp() + " " + message + "\n";
    }
    bufferLog("hello");
    print("from buflog:" + logBuffer);

    // Structured logging (simulated with JSON-like output)
    void structuredLog(const string &in message, dictionary@ attributes = null) {
        string output = "{\"time\":\"" + getTimestamp() + "\",\"level\":\"INFO\",\"msg\":\"" + message + "\"";
        if (attributes !is null) {
            array<string> keys = attributes.getKeys();
            for (uint i = 0; i < keys.length(); i++) {
                string key = keys[i];
                string value = attributes[key];
                output += ",\"" + key + "\":\"" + value + "\"";
            }
        }
        output += "}\n";
        print(output);
    }

    structuredLog("hi there");
    
    dictionary attrs;
    attrs["key"] = "val";
    attrs["age"] = "25";
    structuredLog("hello again", attrs);
}

string getTimestamp() {
    // This is a placeholder. AngelScript doesn't have built-in date/time functions.
    // You would need to implement this based on your specific AngelScript environment.
    return "2023/08/22 10:45:16";
}

This AngelScript code demonstrates various logging techniques, simulating the functionality shown in the original example. Here’s an explanation of the key points:

  1. We use simple print statements for basic logging.

  2. We simulate timestamp logging with a getTimestamp() function. Note that this is a placeholder and would need to be implemented based on your specific AngelScript environment.

  3. File and line information is simulated using the __FILE__ and __LINE__ magic constants.

  4. Custom loggers are implemented as functions that format the log message with prefixes.

  5. Logging to a string buffer is demonstrated with the bufferLog function.

  6. Structured logging is simulated by creating a JSON-like string output.

To run this script, you would need to save it to a file (e.g., logging.as) and use your AngelScript interpreter or compiler to execute it.

Please note that AngelScript doesn’t have built-in logging libraries like Go’s log package. This example demonstrates how you might implement similar functionality using AngelScript’s features. Depending on your specific AngelScript environment, you might have access to additional libraries or functions for more advanced logging capabilities.