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