serval-dna/servaldswift.swift
Andrew Bettison a79156c4d0 Add Swift daemon API, use in 'servaldswift'
The daemon API is built as a Swift module called ServalDNA.

The new CliContext class allows easy implementation of CLI output using
Swift code.  The new CliContextFile subclass is the obvious first
implementation, equivalent to cli_stdio.c.  The 'servaldswift'
executable now uses CliContextFile to print its CLI output to standard
output.

The new delegated log output support constructs each log line in a
buffer and prints it by calling the delegate's 'print' function at
end-of-line.  The 'servaldswift' executable now provides a log output
delegate in Swift that simply prints to standard error, replacing
log_output_console.o, which is omitted from its link.
2018-03-06 15:29:17 +10:30

55 lines
1.7 KiB
Swift

/*
Serval DNA daemon in Swift
Copyright 2017 Flinders University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import Foundation
import ServalDNA
import servald.log
// Logging
//
// A simplistic console log outputter that writes to standard error and is not
// configurable. Note that log_output_console.o is not linked into
// servaldswift, in order to avoid duplicate log outputs on standard error.
private func logPrint(_ level: CInt, _ message: UnsafePointer<CChar>?, _ overrun: Int8) {
let level_text = String(cString: serval_log_level_prefix_string(level)!)
let message_text = String(cString: message!)
FileHandle.standardError.write("\(level_text) \(message_text)\n".data(using:.utf8)!)
}
serval_log_delegate.print = logPrint
serval_log_delegate.minimum_level = LOG_LEVEL_WARN
serval_log_delegate.show_prolog = 1
serval_log_delegate.show_pid = 1
serval_log_delegate.show_time = 1
// Output
var contextFile = CliContextFile(FileHandle.standardOutput)
// Invocation
let status = serval_commandline_main(context: contextFile, args: CommandLine.arguments)
// Cleanup
contextFile.flush()
exit(status)