xref: /petsc/src/sys/logging/handler/interface/lhreg.c (revision ccf0b5c1967b48da6c1c972ce7215de9b66fafa5)
1 
2 #include <petsc/private/petscimpl.h>
3 #include <petsc/private/loghandlerimpl.h>
4 
5 PetscClassId PETSCLOGHANDLER_CLASSID = 0;
6 
7 PetscFunctionList PetscLogHandlerList               = NULL;
8 PetscBool         PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
9 PetscBool         PetscLogHandlerPackageInitialized = PETSC_FALSE;
10 
11 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Default(PetscLogHandler);
12 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(PetscLogHandler);
13 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Trace(PetscLogHandler);
14 #if PetscDefined(HAVE_MPE)
15 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_MPE(PetscLogHandler);
16 #endif
17 #if PetscDefined(HAVE_TAU_PERFSTUBS)
18 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Perfstubs(PetscLogHandler);
19 #endif
20 
21 static PetscErrorCode PetscLogHandlerRegisterAll(void)
22 {
23   PetscFunctionBegin;
24   if (PetscLogHandlerRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
25   PetscLogHandlerRegisterAllCalled = PETSC_TRUE;
26   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_DEFAULT, PetscLogHandlerCreate_Default));
27   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_NESTED, PetscLogHandlerCreate_Nested));
28   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_TRACE, PetscLogHandlerCreate_Trace));
29 #if PetscDefined(HAVE_MPE)
30   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_MPE, PetscLogHandlerCreate_MPE));
31 #endif
32 #if PetscDefined(HAVE_TAU_PERFSTUBS)
33   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_PERFSTUBS, PetscLogHandlerCreate_Perfstubs));
34 #endif
35   PetscFunctionReturn(PETSC_SUCCESS);
36 }
37 
38 /*@C
39   PetscLogHandlerRegister - Register a new `PetscLogHandler`
40 
41   Not collective
42 
43   Input Parameters:
44 + sname    - The name of a new user-defined creation routine
45 - function - The creation routine
46 
47   Example Usage:
48 .vb
49     PetscLogHandlerRegister("my_profiler", MyPetscLogHandlerCreate);
50 .ve
51 
52   Then, your `PetscLogHandler` type can be chosen with the procedural interface via
53 .vb
54     PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
55     PetscLogHandlerSetType(PetscFE, "my_fe");
56 .ve
57 
58   Level: developer
59 
60 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerSetType()`, `PetscLogHandlerGetType()`
61 @*/
62 PetscErrorCode PetscLogHandlerRegister(const char sname[], PetscErrorCode (*function)(PetscLogHandler))
63 {
64   PetscFunctionBegin;
65   PetscCall(PetscFunctionListAdd(&PetscLogHandlerList, sname, function));
66   PetscFunctionReturn(PETSC_SUCCESS);
67 }
68 
69 /*@C
70   PetscLogHandlerSetType - Set the type of a `PetscLogHandler`
71 
72   Input Parameters:
73 + handler - the `PetscLogHandler`
74 - name    - The kind of log handler
75 
76   Level: developer
77 
78 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerGetType()`
79 @*/
80 PetscErrorCode PetscLogHandlerSetType(PetscLogHandler handler, PetscLogHandlerType name)
81 {
82   PetscErrorCode (*r)(PetscLogHandler);
83   PetscBool match;
84 
85   PetscFunctionBegin;
86   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
87   PetscCall(PetscObjectTypeCompare((PetscObject)handler, name, &match));
88   if (match) PetscFunctionReturn(PETSC_SUCCESS);
89 
90   PetscCall(PetscLogHandlerRegisterAll());
91   PetscCall(PetscFunctionListFind(PetscLogHandlerList, name, &r));
92   PetscCheck(r, PetscObjectComm((PetscObject)handler), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscLogHandler type: %s", name);
93 
94   PetscTryTypeMethod(handler, destroy);
95   handler->ops->destroy = NULL;
96 
97   PetscCall((*r)(handler));
98   PetscCall(PetscObjectChangeTypeName((PetscObject)handler, name));
99   PetscFunctionReturn(PETSC_SUCCESS);
100 }
101 
102 /*@C
103   PetscLogHandlerGetType - Gets the `PetscLoagHandlerType` (as a string) from the `PetscLogHandler` object.
104 
105   Not collective
106 
107   Input Parameter:
108 . handler - the `PetscLogHandler`
109 
110   Output Parameter:
111 . name - The `PetscLogHandlerType` name
112 
113   Level: developer
114 
115 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerSetType()`
116 @*/
117 PetscErrorCode PetscLogHandlerGetType(PetscLogHandler handler, PetscLogHandlerType *name)
118 {
119   PetscFunctionBegin;
120   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
121   PetscAssertPointer(name, 2);
122   PetscCall(PetscLogHandlerRegisterAll());
123   PetscCall(PetscObjectGetType((PetscObject)handler, name));
124   PetscFunctionReturn(PETSC_SUCCESS);
125 }
126 
127 static PetscErrorCode PetscLogHandlerFinalizePackage(void)
128 {
129   PetscFunctionBegin;
130   PetscCall(PetscFunctionListDestroy(&PetscLogHandlerList));
131   PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
132   PetscLogHandlerPackageInitialized = PETSC_FALSE;
133   PetscFunctionReturn(PETSC_SUCCESS);
134 }
135 
136 PetscErrorCode PetscLogHandlerPackageInitialize(void)
137 {
138   PetscFunctionBegin;
139   if (PetscLogHandlerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
140   PetscLogHandlerPackageInitialized = PETSC_TRUE;
141 
142   PetscCall(PetscClassIdRegister("Petsc Log Handler", &PETSCLOGHANDLER_CLASSID));
143   PetscCall(PetscLogHandlerRegisterAll());
144   PetscCall(PetscRegisterFinalize(PetscLogHandlerFinalizePackage));
145   {
146     const PetscClassId classids[] = {PETSCLOGHANDLER_CLASSID};
147 
148     PetscCall(PetscInfoProcessClass("loghandler", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
149   }
150   PetscFunctionReturn(PETSC_SUCCESS);
151 }
152