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