libsim Versione 7.2.6
|
◆ l4f_category_log_legacy()
Emit log message for a category with specific priority. Legacy Fortran version that receives an integer instead of a C pointer and a Fortran character argument.
Definizione alla linea 832 del file log4fortran.F90. 833! Copyright (C) 2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
834! authors:
835! Davide Cesari <dcesari@arpa.emr.it>
836! Paolo Patruno <ppatruno@arpa.emr.it>
837
838! This program is free software; you can redistribute it and/or
839! modify it under the terms of the GNU General Public License as
840! published by the Free Software Foundation; either version 2 of
841! the License, or (at your option) any later version.
842
843! This program is distributed in the hope that it will be useful,
844! but WITHOUT ANY WARRANTY; without even the implied warranty of
845! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
846! GNU General Public License for more details.
847
848! You should have received a copy of the GNU General Public License
849! along with this program. If not, see <http://www.gnu.org/licenses/>.
850#include "config.h"
851
852!> \defgroup log4fortran Libsim package, log4fortran library.
853!! Fortran interface to a basic set of log4c library for performing
854!! logging within a program.
855
856!>\brief classe per la gestione del logging
857!!
858!!Questo modulo permette una semplice, ma potente gestione della messagistica.
859!!E' utile sia in fase di debug che di monitoraggio utente.
860!!
861!!Questo modulo fornisce funzionalità simili, ma non identiche a
862!!seconda che siano disponibili in fase di compilazione le librerie
863!!log4c e cnf.
864!!
865!!There are three fundamental types of object in Log4C: categories,
866!!appenders and layouts. You can think of these objects as
867!!corresponding to the what, where and how of the logging system:
868!!categories describe what sub-system the message relates to,
869!!appenders determine where the message goes and layouts determine how
870!!the message is formatted.
871!!
872!!First, you have to figure out what kind of categories you
873!!want. Maybe you want one logger for GUI code and another one for
874!!memory management and a third one for user access
875!!logging. Okay. That's fine. Me, I like to have a separate logger for
876!!each class or data structure. I've already gone to the trouble of
877!!breaking my code down into such categories. Why not just use those?
878!!Feel free to set it up any way you like. Just don't make the mistake
879!!of using message severity as your categories. That's what the
880!!priority is all about.
881!!
882!! La gestione di appenders e layouts viene demandata in toto al file
883!! di configurazione di log4c (vedere apposita documentazione
884!! http://log4c.cvs.sourceforge.net/\*checkout\*/log4c/log4c/doc/Log4C-DevelopersGuide.odt )
885!!
886!!log4fortran by default can log messages with some standard priority levels:
887!!
888!!Use debug to write debugging messages which should not be printed
889!!when the application is in production.
890!!
891!!Use info for messages similar to the "verbose" mode of many
892!!applications.
893!!
894!!Use warn for warning messages which are logged to some log but the
895!!application is able to carry on without a problem.
896!!
897!!Use error for application error messages which are also logged to
898!!some log but, still, the application can hobble along. Such as when
899!!some administrator-supplied configuration parameter is incorrect and
900!!you fall back to using some hard-coded default value.
901!!
902!!Use fatal for critical messages, after logging of which the
903!!application quits abnormally.
904!!
905!!Configuration syntax:
906!!
907!!The log4crc configuration file uses an XML syntax. The root element
908!!is <log4c> and it can be used to control the configuration file
909!!version interface with the attribute "version". The following 4
910!!elements are supported: <config>, <category>, <appender> and
911!!<layout>.
912!!
913!! The <config> element controls the global log4c
914!! configuration. It has 3 sub elements. The <nocleanup> flag
915!! inhibits the log4c destructors routines. The <bufsize> element
916!! sets the buffer size used to format log4c_logging_event_t
917!! objects. If is set to 0, the allocation is dynamic (the <debug>
918!! element is currently unused).
919!!
920!! The <category> element has 3 possible attributes: the category
921!! "name", the category "priority" and the category
922!! "appender". Future versions will handle multple appenders per
923!! category.
924!!
925!! The <appender> element has 3 possible attributes: the appender
926!! "name", the appender "type", and the appender "layout".
927!!
928!! The <layout> element has 2 possible attributes: the layout
929!! "name" and the layout "type".
930!!
931!!
932!!This initial version of the log4c configuration file syntax is quite
933!!different from log4j. XML seemed the best choice to keep the log4j
934!!configuration power in a C API. Environment variables
935!!
936!! LOG4C_RCPATH holds the path to the main log4crc configuration file
937!! LOG4C_PRIORITY holds the "root" category priority
938!! LOG4C_APPENDER holds the "root" category appender
939!!
940!!
941!!Programma esempio \include log4fortran.f90
942!!Here's one sample log4crc configuration file \include log4crc
943!!
944!!\ingroup log4fortran
946USE iso_c_binding
947IMPLICIT NONE
948
949INTEGER(kind=c_int),PARAMETER :: L4F_FATAL = 000 !< standard priority
950INTEGER(kind=c_int),PARAMETER :: L4F_ALERT = 100 !< standard priority
951INTEGER(kind=c_int),PARAMETER :: L4F_CRIT = 200 !< standard priority
952INTEGER(kind=c_int),PARAMETER :: L4F_ERROR = 300 !< standard priority
953INTEGER(kind=c_int),PARAMETER :: L4F_WARN = 400 !< standard priority
954INTEGER(kind=c_int),PARAMETER :: L4F_NOTICE = 500 !< standard priority
955INTEGER(kind=c_int),PARAMETER :: L4F_INFO = 600 !< standard priority
956INTEGER(kind=c_int),PARAMETER :: L4F_DEBUG = 700 !< standard priority
957INTEGER(kind=c_int),PARAMETER :: L4F_TRACE = 800 !< standard priority
958INTEGER(kind=c_int),PARAMETER :: L4F_NOTSET = 900 !< standard priority
959INTEGER(kind=c_int),PARAMETER :: L4F_UNKNOWN = 1000 !< standard priority
960
961!> Default priority value. It is used only when compiled without log4c
962!! since the configuration file is ignored, but it is better to define
963!! it all the time.
964INTEGER(kind=c_int),PUBLIC :: l4f_priority=l4f_notice
965
966!> l4f handle. This type defines an opaque handle
967!! to a l4f category (mapped to a log4c category),
968!! it has to be initialised with the l4f_category_get method.
969TYPE,BIND(C) :: l4f_handle
970 PRIVATE
971 TYPE(c_ptr) :: ptr = c_null_ptr
973
974#ifdef HAVE_LIBLOG4C
975
976TYPE(l4f_handle),SAVE :: l4f_global_default
977
978! emulation of old cnf behavior returning integer instead of pointer
979#undef ARRAYOF_ORIGEQ
980#undef ARRAYOF_ORIGTYPE
981#undef ARRAYOF_TYPE
982#define ARRAYOF_ORIGTYPE TYPE(l4f_handle)
983#define ARRAYOF_TYPE arrayof_l4f_handle
984#include "arrayof_pre_nodoc.F90"
985
986TYPE(arrayof_l4f_handle) :: l4f_global_ptr
987
988!> Global log4fortran constructor.
989INTERFACE
991 IMPORT
992 INTEGER(kind=c_int) :: l4f_init
994END INTERFACE
995
996!> Initialize a logging category. This is the C version, please use the
997!! Fortran version l4f_category_get that receives a Fortran character.
998INTERFACE
1000 IMPORT
1001 CHARACTER(kind=c_char),INTENT(in) :: a_name(*) !< category name
1002 TYPE(l4f_handle) :: l4f_category_get_c
1004END INTERFACE
1005
1006!! Delete a logging category. It can receive a C pointer or a
1007!! legacy integer value.
1008INTERFACE l4f_category_delete
1009! SUBROUTINE l4f_category_delete_c(a_category) BIND(C,name='log4c_category_delete')
1010! IMPORT
1011! TYPE(l4f_handle),VALUE :: a_category !< category as C native pointer
1012! END SUBROUTINE l4f_category_delete_c
1013 MODULE PROCEDURE l4f_category_delete_legacy, l4f_category_delete_f
1014END INTERFACE
1015! this function has been disabled because aftere deleting a category
1016! the following log4c_fini fails with a double free, we must
1017! understand the log4c docs
1018
1019INTERFACE
1020 SUBROUTINE l4f_category_log_c(a_category, a_priority, a_format) bind(C,name='log4c_category_log_c')
1021 IMPORT
1022 TYPE(l4f_handle),VALUE :: a_category !< category
1023 INTEGER(kind=c_int),VALUE :: a_priority !< priority level
1024! TYPE(c_ptr),VALUE :: locinfo !< not used
1025 CHARACTER(kind=c_char),INTENT(in) :: a_format(*) !< message to emit
1026 ! TYPE(c_ptr),VALUE :: a_args
1027 END SUBROUTINE l4f_category_log_c
1028END INTERFACE
1029
1030!> Emit log message for a category with specific priority.
1031!! It can receive a C pointer or a legacy integer value.
1033 MODULE PROCEDURE l4f_category_log_f, l4f_category_log_legacy
1035
1036!> Return true if the corresponding category handle exists.
1038 MODULE PROCEDURE l4f_category_exist_f, l4f_category_exist_legacy
1040
1041!> log4fortran destructor
1042INTERFACE
1044 IMPORT
1045 INTEGER(kind=c_int) :: l4f_fini
1047END INTERFACE
1048
1049!>Ritorna un messaggio caratteristico delle priorità standard
1050!interface
1051!CHARACTER(len=12) FUNCTION l4f_msg(a_priority)
1052!integer,intent(in):: a_priority !< category name
1053!end function l4f_msg
1054!end interface
1055
1056#else
1057
1058CHARACTER(len=510),PRIVATE:: dummy_a_name
1059
1060#endif
1061
1062PRIVATE
1063PUBLIC l4f_fatal, l4f_alert, l4f_crit, l4f_error, l4f_warn, l4f_notice, &
1064 l4f_info, l4f_debug, l4f_trace, l4f_notset, l4f_unknown
1067PUBLIC l4f_launcher
1068
1069CONTAINS
1070
1071!> Routine specifica per il SIM. Cattura le variabili di ambiente
1072!! LOG4_APPLICATION_NAME,LOG4_APPLICATION_ID e compone il nome univoco
1073!! per il logging. Se le variabili di ambiente non sono impostate
1074!! ritorna un nome definito dal nome del processo e da un timestamp.
1075SUBROUTINE l4f_launcher(a_name, a_name_force, a_name_append)
1076CHARACTER(len=*),INTENT(out) :: a_name !< nome univoco per logging
1077CHARACTER(len=*),INTENT(in),OPTIONAL :: a_name_force !< forza il valore di a_name
1078CHARACTER(len=*),INTENT(in),OPTIONAL :: a_name_append !< valore da appendere a a_name
1079
1080INTEGER :: tarray(8)
1081CHARACTER(len=255) :: LOG4_APPLICATION_NAME,LOG4_APPLICATION_ID,arg
1082CHARACTER(len=255),SAVE :: a_name_save=""
1083
1084IF (PRESENT(a_name_force))THEN
1085 a_name=a_name_force
1086ELSE IF (a_name_save /= "")THEN
1087 a_name=a_name_save
1088ELSE
1089
1090 CALL date_and_time(values=tarray)
1091 CALL getarg(0, arg)
1092 CALL getenv("LOG4_APPLICATION_NAME", log4_application_name)
1093 CALL getenv("LOG4_APPLICATION_ID", log4_application_id)
1094
1095 IF (log4_application_name == "" .AND. log4_application_id == "") THEN
1096 WRITE(a_name,"(a,a,8i5,a)")trim(arg),"[",tarray,"]"
1097 ELSE
1098 a_name = trim(log4_application_name)//"["//trim(log4_application_id)//"]"
1099 END IF
1100
1101END IF
1102
1103a_name_save=a_name
1104
1105IF (PRESENT(a_name_append)) THEN
1106 a_name=trim(a_name)//"."//trim(a_name_append)
1107END IF
1108
1109END SUBROUTINE l4f_launcher
1110
1111#ifndef HAVE_LIBLOG4C
1112! definisce delle dummy routine
1113
1114!> log4fortran constructor
1116
1117character(len=10)::priority
1118integer :: iostat
1119
1120call getenv("LOG4C_PRIORITY",priority)
1121if (priority=="") then
1122 l4f_priority = l4f_notice
1123else
1124 read(priority,*,iostat=iostat)l4f_priority
1125end if
1126
1127if (iostat /= 0) then
1128 l4f_priority = l4f_notice
1129end if
1130
1131l4f_init = 0
1132
1134
1135
1136!>Initialize a logging category.
1137integer function l4f_category_get (a_name)
1138character (len=*),intent(in) :: a_name !< category name
1139
1140dummy_a_name = a_name
1141l4f_category_get = 1
1142
1143end function l4f_category_get
1144
1145
1146!>Delete a logging category.
1147subroutine l4f_category_delete(a_category)
1148integer,intent(in):: a_category !< category name
1149
1150if (a_category == 1) dummy_a_name = ""
1151
1152end subroutine l4f_category_delete
1153
1154
1155!>Emit log message for a category with specific priority
1157integer,intent(in):: a_category !< category name
1158integer,intent(in):: a_priority !< priority level
1159character(len=*),intent(in):: a_format !< message to emit
1160
1161if (a_category == 1 .and. a_priority <= l4f_priority) then
1162 write(*,*)"[dummy] ",l4f_msg(a_priority),trim(dummy_a_name)," - ",trim(a_format)
1163end if
1164
1166
1167
1168!>Emit log message without category with specific priority
1169subroutine l4f_log (a_priority,a_format)
1170integer,intent(in):: a_priority !< priority level
1171character(len=*),intent(in):: a_format !< message to emit
1172
1173if ( a_priority <= l4f_priority) then
1174 write(*,*)"[_default] ",l4f_msg(a_priority),trim(dummy_a_name)," - ",trim(a_format)
1175end if
1176
1177end subroutine l4f_log
1178
1179
1180!>Return True if category exist
1182integer,intent(in):: a_category !< category name
1183
1184if (a_category == 1) then
1185 l4f_category_exist= .true.
1186else
1187 l4f_category_exist= .false.
1188end if
1189
1191
1192
1193!>log4fortran destructors
1195
1196l4f_fini= 0
1197
1199
1200!>Ritorna un messaggio caratteristico delle priorità standard
1201character(len=12) function l4f_msg(a_priority)
1202
1203integer,intent(in):: a_priority !< category name
1204
1205write(l4f_msg,*)a_priority
1206
1207if (a_priority == l4f_fatal) l4f_msg="FATAL"
1208if (a_priority == l4f_alert) l4f_msg="ALERT"
1209if (a_priority == l4f_crit) l4f_msg="CRIT"
1210if (a_priority == l4f_error) l4f_msg="ERROR"
1211if (a_priority == l4f_warn) l4f_msg="WARN"
1212if (a_priority == l4f_notice) l4f_msg="NOTICE"
1213if (a_priority == l4f_info) l4f_msg="INFO"
1214if (a_priority == l4f_debug) l4f_msg="DEBUG"
1215if (a_priority == l4f_trace) l4f_msg="TRACE"
1216if (a_priority == l4f_notset) l4f_msg="NOTSET"
1217if (a_priority == l4f_unknown) l4f_msg="UNKNOWN"
1218
1219end function l4f_msg
1220
1221#else
1222
1223#include "arrayof_post_nodoc.F90"
1224
1225!> Initialize a logging category. This is the
1226!! Fortran legacy version that receives a Fortran character argument
1227!! and returns an integer.
1228FUNCTION l4f_category_get(a_name) RESULT(handle)
1229CHARACTER(kind=c_char,len=*),INTENT(in) :: a_name !< category name
1230INTEGER :: handle
1231
1232INTEGER :: i
1233
1234DO i = 1, l4f_global_ptr%arraysize ! look first for a hole
1236 l4f_global_ptr%array(i) = l4f_category_get_c(trim(a_name)//char(0))
1237 handle = i
1238 RETURN
1239 ENDIF
1240ENDDO
1241
1242handle = append(l4f_global_ptr, l4f_category_get_c(trim(a_name)//char(0)))
1243
1244END FUNCTION l4f_category_get
1245
1246
1247!> Initialize a logging category. This is the
1248!! Fortran version that receives a Fortran character argument
1249!! and returns a typed handle.
1250FUNCTION l4f_category_get_handle(a_name) RESULT(handle)
1251CHARACTER(kind=c_char,len=*),INTENT(in) :: a_name !< category name
1252TYPE(l4f_handle) :: handle
1253
1254handle = l4f_category_get_c(trim(a_name)//char(0))
1255
1256END FUNCTION l4f_category_get_handle
1257
1258
1259!> Delete a logging category. Legacy version with an integer argument.
1260SUBROUTINE l4f_category_delete_legacy(a_category)
1261INTEGER,INTENT(in) :: a_category !< category as an integer
1262
1263IF (a_category <= 0 .OR. a_category > l4f_global_ptr%arraysize) RETURN
1264IF (a_category == l4f_global_ptr%arraysize) THEN
1265 CALL remove(l4f_global_ptr, pos=a_category)
1266ELSE
1267 l4f_global_ptr%array(a_category)%ptr = c_null_ptr
1268ENDIF
1269
1270END SUBROUTINE l4f_category_delete_legacy
1271
1272
1273!> Delete a logging category. No-op version with a typed handle.
1274SUBROUTINE l4f_category_delete_f(a_category)
1275TYPE(l4f_handle),INTENT(inout) :: a_category !< category as C native pointer
1276
1277a_category%ptr = c_null_ptr ! is it necessary?
1278
1279END SUBROUTINE l4f_category_delete_f
1280
1281
1282!> Emit log message for a category with specific priority.
1283!! Fortran version that receives a Fortran character argument.
1284SUBROUTINE l4f_category_log_f(a_category, a_priority, a_format)
1285TYPE(l4f_handle),INTENT(in) :: a_category !< category
1286INTEGER(kind=c_int),INTENT(in) :: a_priority !< priority level
1287CHARACTER(len=*),INTENT(in) :: a_format !< message to emit
1288
1289CALL l4f_category_log_c(a_category, a_priority, trim(a_format)//char(0))
1290
1291END SUBROUTINE l4f_category_log_f
1292
1293
1294!> Emit log message for a category with specific priority.
1295!! Legacy Fortran version that receives an integer instead of a C
1296!! pointer and a Fortran character argument.
1297SUBROUTINE l4f_category_log_legacy(a_category, a_priority, a_format)
1298INTEGER(kind=c_int),INTENT(in) :: a_category !< category
1299INTEGER(kind=c_int),INTENT(in) :: a_priority !< priority level
1300CHARACTER(len=*),INTENT(in) :: a_format !< message to emit
1301
1302CALL l4f_category_log_c(l4f_global_ptr%array(a_category), a_priority, trim(a_format)//char(0))
1303
1304END SUBROUTINE l4f_category_log_legacy
1305
1306
1307!> Emit log message without category with specific priority.
1308!! Fortran version that receives a Fortran character argument.
1309SUBROUTINE l4f_log(a_priority, a_format)
1310INTEGER(kind=c_int),INTENT(in) :: a_priority !< priority level
1311CHARACTER(len=*),INTENT(in) :: a_format !< message to emit
1312
1313INTEGER :: i
1314
1316 i = l4f_init()
1317 l4f_global_default = l4f_category_get_handle('_default')
1318ENDIF
1320
1321END SUBROUTINE l4f_log
1322
1323
1324!> Return true if the corresponding category handle exists
1325!! (is associated with a category).
1326FUNCTION l4f_category_exist_f(a_category) RESULT(exist)
1327TYPE(l4f_handle),INTENT(in) :: a_category !< category
1328LOGICAL :: exist
1329
1330exist = c_associated(a_category%ptr)
1331
1332END FUNCTION l4f_category_exist_f
1333
1334!> Return true if the corresponding category handle exists
1335!! (is associated with a category).
1336!! Legacy Fortran version that receives an integer instead of a C
1337!! pointer.
1338FUNCTION l4f_category_exist_legacy(a_category) RESULT(exist)
1339INTEGER,INTENT(in):: a_category !< category
1340LOGICAL :: exist
1341
1342IF (a_category <= 0 .OR. a_category > l4f_global_ptr%arraysize) THEN
1343 exist = .false.
1344ELSE
1345 exist = l4f_category_exist(l4f_global_ptr%array(a_category))
1346ENDIF
1347
1348END FUNCTION l4f_category_exist_legacy
1349
1350
1351#endif
1352
Return true if the corresponding category handle exists. Definition log4fortran.F90:462 Emit log message for a category with specific priority. Definition log4fortran.F90:457 |