diff options
author | Björn Persson <Bjorn@Rombobjörn.se> | 2017-05-06 22:32:13 +0200 |
---|---|---|
committer | Björn Persson <Bjorn@Rombobjörn.se> | 2017-05-06 22:32:13 +0200 |
commit | e3a7fe3a3b273c2660f25c34c99faf6047b85c07 (patch) | |
tree | a949f514181930a65d85e1b93016614bb6b1974f | |
parent | ab788c1b4841a58d8807b5846d1ee9323142ee9c (diff) |
Fixed Set_Log_Levels on big-endian platforms.HEADversion_1.6master
-rw-r--r-- | system_log.adb | 39 | ||||
-rw-r--r-- | test/test_system_log.adb | 6 |
2 files changed, 28 insertions, 17 deletions
diff --git a/system_log.adb b/system_log.adb index 2755501..e77c95d 100644 --- a/system_log.adb +++ b/system_log.adb @@ -1,5 +1,5 @@ -- System_Log, a binding to the Unix syslog functions --- Copyright 2009 - 2013 B. Persson, Bjorn@Rombobeorn.se +-- Copyright 2009 - 2017 B. Persson, Bjorn@Rombobeorn.se -- -- This library is free software: you can redistribute it and/or modify it -- under the terms of the GNU General Public License version 3, as published @@ -7,12 +7,13 @@ with Interfaces.C.Strings; use Interfaces.C; use Interfaces.C.Strings; -with Ada.Unchecked_Conversion; package body System_Log is - Facility_Numbers : constant array(Log_Facility) of int := + subtype Facility_Number is int range 0 .. 23 * 8; + + Facility_Numbers : constant array(Log_Facility) of Facility_Number := (Kernel => 0 * 8, -- LOG_KERN User => 1 * 8, -- LOG_USER Mail => 2 * 8, -- LOG_MAIL @@ -33,7 +34,9 @@ package body System_Log is Local6 => 22 * 8, -- LOG_LOCAL6 Local7 => 23 * 8); -- LOG_LOCAL7 - Level_Numbers : constant array(Log_Level) of int := + subtype Level_Number is Natural range 0 .. 7; + + Level_Numbers : constant array(Log_Level) of Level_Number := (Emergency => 0, -- LOG_EMERG Alert => 1, -- LOG_ALERT Critical => 2, -- LOG_CRIT @@ -98,17 +101,21 @@ package body System_Log is function setlogmask(mask : int) return int; pragma Import(C, setlogmask, "setlogmask"); - Bits : constant := Log_Level'Pos(Log_Level'Last) + 1; - type Mask is range 0 .. 2 ** Bits - 1; - for Mask'Size use Bits; - function To_Mask is new Ada.Unchecked_Conversion(Source => Log_Levels, - Target => Mask); - function To_Levels is new Ada.Unchecked_Conversion(Source => Mask, - Target => Log_Levels); + New_Mask : int := 0; + Old_Mask : unsigned; begin - -- Convert the input array of Boolean to a number, pass that to - -- setlogmask, and convert the output in the other direction. - Old_Levels := To_Levels(Mask(setlogmask(int(To_Mask(New_Levels))))); + -- Convert the input array of Boolean to a number. + for L in Log_Levels'Range loop + if New_Levels(L) then + New_Mask := New_Mask + 2 ** Level_Numbers(L); + end if; + end loop; + -- Pass that number to setlogmask and get the previous mask back. + Old_Mask := unsigned(setlogmask(New_Mask)); + -- Convert the output number to an array of Boolean. + for L in Log_Levels'Range loop + Old_Levels(L) := (Old_Mask and 2 ** Level_Numbers(L)) /= 0; + end loop; end Set_Log_Levels; @@ -131,7 +138,7 @@ package body System_Log is procedure Log(Level : in Log_Level; Message : in String) is begin - syslog(Level_Numbers(Level), Simple_Format, To_C(Message)); + syslog(int(Level_Numbers(Level)), Simple_Format, To_C(Message)); end Log; @@ -140,7 +147,7 @@ package body System_Log is Message : in String) is begin - syslog(Facility_Numbers(Facility) + Level_Numbers(Level), + syslog(Facility_Numbers(Facility) + int(Level_Numbers(Level)), Simple_Format, To_C(Message)); end Log; diff --git a/test/test_system_log.adb b/test/test_system_log.adb index 319c039..5a0a053 100644 --- a/test/test_system_log.adb +++ b/test/test_system_log.adb @@ -1,5 +1,5 @@ -- This is a simple test program for testing System_Log. --- Copyright 2012 B. Persson, Bjorn@Rombobeorn.se +-- Copyright 2009 - 2017 B. Persson, Bjorn@Rombobeorn.se -- -- This program is free software: you can redistribute it and/or modify it -- under the terms of the GNU General Public License version 3, as published @@ -14,4 +14,8 @@ begin Include_PID => True); Log(Info, "This is a test message on the info level."); Log(Debug, "This is a test message on the debug level."); + Set_Log_Threshold(Info); + Log(Debug, "This test message is below the threshold and shouldn't be seen."); + Log(Info, "This test message is above the threshold and should be seen."); + Log(Local5, Info, "This is a test message logged as the Local5 facility."); end Test_System_Log; |