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