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