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