//
//  StreamFormattable.swift
//  Tambo
//
//  Created by Massimo Donati on 8/9/18.
//

import Foundation

public protocol StreamFormattable: StreamProtocol {
    associatedtype FormattingType: LogFormatterProtocol
    /**
     The Object responsible to convert the Tlog object into whatever the stream
     is expecting it to be.
     */
    var logFormatter: FormattingType {get set}

    /**
     Output the log to the stream i.e. file, backend, db etc.
     - parameter log: The [Log](x-source-tag://T.Log) object generated by the
     logger.
     - parameter formattedLog: The formatted log ready for output.
     - important: the default implementation will throw an exception since every
     stream should define it's own implementation.
     */
    func output(log: Log, formattedLog: FormattingType.FormattedType)
}

extension StreamFormattable {
    public func process(_ log: Log) {
        let processClosure = {
            let formattedLog = self.logFormatter.format(log)
            self.output(log: log, formattedLog: formattedLog)
        }
        if isAsync {
            queue.async(execute: processClosure)
        } else {
            processClosure()
        }
    }
}
