Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-huxtable committed Aug 21, 2018
0 parents commit 4ca1cdf
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/doc/
/docs/
/lib/
/bin/
/.shards/

/shard.lock
.DS_Store
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
6 changes: 6 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ISC License (ISC)
Copyright 2018 Christian Huxtable <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# syslog.cr

Adds syslog functionality to crystal.


## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
syslog:
github: chris-huxtable/syslog.cr
```
## Usage
Requiring:
```crystal
require "syslog"
```

Basic Usage:

```crystal
Syslog.emergency("This is a notice message")
Syslog.alert("This is a notice message")
Syslog.critical("This is a notice message")
Syslog.error("This is a notice message")
Syslog.warning("This is a notice message")
Syslog.notice("This is a notice message")
Syslog.info("This is a info message")
Syslog.debug("This is a debug message")
```

Facility Usage:

```crystal
Syslog.info("This is a info message sent to the LOCAL0 Facility", Syslog::Facility::Local0)
```


## Contributing

1. Fork it (<https://github.com/chris-huxtable/syslog.cr/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request


## Contributors

- [Chris Huxtable](https://github.com/chris-huxtable) - creator, maintainer
8 changes: 8 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: syslog
version: 0.1.0
license: ISC

crystal: 0.25.1

authors:
- Chris Huxtable <[email protected]>
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/syslog"
169 changes: 169 additions & 0 deletions src/syslog.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Copyright (c) 2018 Christian Huxtable <[email protected]>.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

require "./syslog/*"


module Syslog


# MARK: - Defaults

@@facility : Facility = Facility::User
class_property facility : Facility

def self.mask=(mask : Int) : Nil
LibC.setlogmask(mask)
end


# MARK: - Generics

# Initializes the log file for special processing.
# The use of this method is optional.
#
# NOTE: This API may be replaced in a future version.
def self.open(prefix : String, facility : Facility = @@facility, *options : Option) : Nil
@@facility = facility
options = options.reduce(0) { |acc, elm| (acc|elm.value) }
LibC.openlog(prefix, options, facility)

# syslog_data = syslog_data_init()
# LibC.openlog_r(prefix, options, facility, pointerof(syslog_data))
end

# :ditto:
def self.open(prefix : String, facility : Facility = @@default_facility) : Nil
LibC.openlog(prefix, 0, facility)

# syslog_data = syslog_data_init()
# LibC.openlog_r(prefix, 0, facility, pointerof(syslog_data))
end

# Writes a message with a priority and facility.
def self.log(priority : Priority, facility : Facility, message : String) : Nil
LibC.syslog(priority.value|facility.value, message)

# syslog_data = syslog_data_init()
# LibC.syslog_r(priority.value|facility.value, pointerof(syslog_data), message)
end

# Closes the descriptor being used to write to the system logger, clearing special options.
# The use of this method is optional.
#
# NOTE: This API may be replaced in a future version.
def self.close() : Nil
@@facility = Facility::User
LibC.closelog()

# syslog_data = syslog_data_init()
# LibC.closelog_r(pointerof(syslog_data))
end


# MARK: - Log By Level

# Writes an emergency *syslog* message with an optional facility.
def self.emergency(message : String, facility : Facility = @@facility) : Nil
log(Priority::Emergency, facility, message)
end

# Writes an alert *syslog* message with an optional facility.
def self.alert(message : String, facility : Facility = @@facility) : Nil
log(Priority::Alert, facility, message)
end

# Writes an critical *syslog* message with an optional facility.
def self.critical(message : String, facility : Facility = @@facility) : Nil
log(Priority::Critical, facility, message)
end

# Writes an error *syslog* message with an optional facility.
def self.error(message : String, facility : Facility = @@facility) : Nil
log(Priority::Error, facility, message)
end

# Writes an warning *syslog* message with an optional facility.
def self.warning(message : String, facility : Facility = @@facility) : Nil
log(Priority::Warning, facility, message)
end

# Writes an notice *syslog* message with an optional facility.
def self.notice(message : String, facility : Facility = @@facility) : Nil
log(Priority::Notice, facility, message)
end

# Writes an info *syslog* message with an optional facility.
def self.info(message : String, facility : Facility = @@facility) : Nil
log(Priority::Info, facility, message)
end

# Writes an debug *syslog* message with an optional facility.
def self.debug(message : String, facility : Facility = @@facility) : Nil
log(Priority::Debug, facility, message)
end


# MARK: - Enums

enum Facility
Authorization = LibC::LOG_AUTH
Privilage = LibC::LOG_AUTHPRIV
Cron = LibC::LOG_CRON
Daemon = LibC::LOG_DAEMON
FTP = LibC::LOG_FTP
Kernal = LibC::LOG_KERN
LPR = LibC::LOG_LPR
Mail = LibC::LOG_MAIL
News = LibC::LOG_NEWS
Syslog = LibC::LOG_SYSLOG
User = LibC::LOG_USER
UUCP = LibC::LOG_UUCP
Local0 = LibC::LOG_LOCAL0
Local1 = LibC::LOG_LOCAL1
Local2 = LibC::LOG_LOCAL2
Local3 = LibC::LOG_LOCAL3
Local4 = LibC::LOG_LOCAL4
Local5 = LibC::LOG_LOCAL5
Local6 = LibC::LOG_LOCAL6
Local7 = LibC::LOG_LOCAL7
end

enum Priority
Emergency = LibC::LOG_EMERG
Alert = LibC::LOG_ALERT
Critical = LibC::LOG_CRIT
Error = LibC::LOG_ERR
Warning = LibC::LOG_WARNING
Notice = LibC::LOG_NOTICE
Info = LibC::LOG_INFO
Debug = LibC::LOG_DEBUG
end

enum Options
Console = LibC::LOG_CONS
NoDelay = LibC::LOG_NDELAY
Delay = LibC::LOG_ODELAY
PrintError = LibC::LOG_PERROR
PID = LibC::LOG_PID
end


# MARK: - Utilities

# macro syslog_data_init()
# LibC::SyslogData.new(log_stat: 0, log_tag: "", log_fac: LibC::LOG_USER, log_mask: 0xff)
# end

end
92 changes: 92 additions & 0 deletions src/syslog/lib_c.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright (c) 2018 Christian Huxtable <[email protected]>.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


lib LibC

# MARK: - Log Priority

LOG_EMERG = 0 # system is unusable
LOG_ALERT = 1 # action must be taken immediately
LOG_CRIT = 2 # critical conditions
LOG_ERR = 3 # error conditions
LOG_WARNING = 4 # warning conditions
LOG_NOTICE = 5 # normal but significant condition
LOG_INFO = 6 # informational
LOG_DEBUG = 7 # debug-level messages


# MARK: - Log Facility

LOG_KERN = (0 << 3) # kernel messages
LOG_USER = (1 << 3) # random user-level messages
LOG_MAIL = (2 << 3) # mail system
LOG_DAEMON = (3 << 3) # system daemons
LOG_AUTH = (4 << 3) # security/authorization messages
LOG_SYSLOG = (5 << 3) # messages generated internally by syslogd
LOG_LPR = (6 << 3) # line printer subsystem
LOG_NEWS = (7 << 3) # network news subsystem
LOG_UUCP = (8 << 3) # UUCP subsystem
LOG_CRON = (9 << 3) # clock daemon
LOG_AUTHPRIV = (10 << 3) # security/authorization messages (private)
LOG_FTP = (11 << 3) # ftp daemon

LOG_LOCAL0 = (16 << 3) # reserved for local use
LOG_LOCAL1 = (17 << 3) # reserved for local use
LOG_LOCAL2 = (18 << 3) # reserved for local use
LOG_LOCAL3 = (19 << 3) # reserved for local use
LOG_LOCAL4 = (20 << 3) # reserved for local use
LOG_LOCAL5 = (21 << 3) # reserved for local use
LOG_LOCAL6 = (22 << 3) # reserved for local use
LOG_LOCAL7 = (23 << 3) # reserved for local use


# MARK: - Log Options

LOG_PID = 0x01 # log the pid with each message
LOG_CONS = 0x02 # log on the console if errors in sending
LOG_ODELAY = 0x04 # delay open until first syslog() (default)
LOG_NDELAY = 0x08 # don't delay open
LOG_NOWAIT = 0x10 # don't wait for console forks: DEPRECATED
LOG_PERROR = 0x20 # log to stderr as well


# MARK: - Syslog Data

# {% if flag?(:openbsd) || flag?(:freebsd) %}
# struct SyslogData
# log_stat : Int
# log_tag : Char*
# log_fac : Int
# log_mask : Int
# end
# {% end %}


# MARK: - Functions

fun syslog(priority : Int, message : Char*, ...) : Void
fun openlog(ident : Char*, logopt : Int, facility : Int) : Void
fun closelog() : Void
fun setlogmask(maskpri : Int) : Int

# {% if flag?(:openbsd) || flag?(:freebsd) %}
# fun syslog_r(priority : Int, data : SyslogData*, message : Char*, ...) : Void
# fun openlog_r(ident : Char*, logopt : Int, facility : Int, data : SyslogData*) : Void
# fun closelog_r(data : SyslogData*) : Void
# fun setlogmask_r(maskpri : Int, data : SyslogData*) : Int
# {% end %}

end

0 comments on commit 4ca1cdf

Please sign in to comment.